LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code



CSS
html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}
body {
  background: #fff;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
.js-transitions-disabled * {
  -webkit-transition: none !important;
  transition: none !important;
}
.carousel {
  position: relative;
  height: 100%;
  overflow: hidden;
  -webkit-perspective: 50vw;
          perspective: 50vw;
  -webkit-perspective-origin: 50% 50%;
          perspective-origin: 50% 50%;
}
.carousel__control {
  position: absolute;
  height: 160px;
  width: 40px;
  background: #fff;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  z-index: 1;
}
.carousel__control a {
  position: relative;
  display: block;
  width: 100%;
  padding-top: 75%;
  box-sizing: border-box;
}
.carousel__control a:hover:before {
  background-color: rgba(0,0,0,0.4);
}
.carousel__control a.active:before,
.carousel__control a.active:hover:before {
  background-color: rgba(0,0,0,0.6);
}
.carousel__control a:first-child {
  margin-top: 15px;
}
.carousel__control a:before {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  margin: auto;
  border-radius: 50%;
  padding-top: 25%;
  width: 25%;
  background: rgba(0,0,0,0.2);
  content: '';
  display: block;
  margin-top: -12.5%;
}
.carousel__stage {
  position: absolute;
  top: 20px;
  bottom: 20px;
  left: 20px;
  right: 20px;
  margin: auto;
  -webkit-transform-style: preserve-3d;
          transform-style: preserve-3d;
  -webkit-transform: translateZ(calc(-50vh + 20px));
          transform: translateZ(calc(-50vh + 20px));
}
.spinner {
  position: absolute;
  width: calc(50vw - (20px));
  height: calc(100vh - 40px);
  top: 0;
  left: 0;
  right: auto;
  bottom: 0;
  margin: auto;
  -webkit-transform-style: preserve-3d;
          transform-style: preserve-3d;
  -webkit-transition: -webkit-transform 1s;
  transition: -webkit-transform 1s;
  transition: transform 1s;
  transition: transform 1s, -webkit-transform 1s;
  -webkit-backface-visibility: hidden;
          backface-visibility: hidden;
  -webkit-transform-origin: 50% 50%;
          transform-origin: 50% 50%;
  -webkit-transform: rotateX(0);
          transform: rotateX(0);
}
.js-spin-fwd .spinner {
  -webkit-transform: rotateX(-90deg);
          transform: rotateX(-90deg);
}
.js-spin-bwd .spinner {
  -webkit-transform: rotateX(90deg);
          transform: rotateX(90deg);
}
.js-spin-fwd .spinner--right {
  -webkit-transform: rotateX(90deg);
          transform: rotateX(90deg);
}
.js-spin-bwd .spinner--right {
  -webkit-transform: rotateX(-90deg);
          transform: rotateX(-90deg);
}
.spinner--right {
  right: 0;
  left: auto;
}
.spinner__face {
  display: none;
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.spinner__face.js-next {
  display: block;
  -webkit-transform: rotateX(90deg) translateZ(calc(50vh - 20px));
          transform: rotateX(90deg) translateZ(calc(50vh - 20px));
}
.spinner--right .spinner__face.js-next {
  -webkit-transform: rotateX(270deg) translateZ(calc(50vh - 20px));
          transform: rotateX(270deg) translateZ(calc(50vh - 20px));
}
.js-spin-bwd .spinner__face.js-next {
  -webkit-transform: rotateX(-90deg) translateZ(calc(50vh - 20px));
          transform: rotateX(-90deg) translateZ(calc(50vh - 20px));
}
.js-spin-bwd .spinner--right .spinner__face.js-next {
  -webkit-transform: rotateX(-270deg) translateZ(calc(50vh - 20px));
          transform: rotateX(-270deg) translateZ(calc(50vh - 20px));
}
.js-active {
  display: block;
  -webkit-transform: translateZ(calc(50vh - 20px));
          transform: translateZ(calc(50vh - 20px));
}
.content {
  position: absolute;
  width: 200%;
  height: 100%;
  left: 0;
}
.spinner--right .content {
  left: -100%;
}
.content__left,
.content__right {
  position: absolute;
  left: 0;
  top: 0;
  width: 50%;
  height: 100%;
}
.content__right {
  right: 0;
  left: auto;
}
.content__left {
  background-repeat: no-repeat;
  background-size: cover;
}
.content__left:after {
  position: absolute;
  display: block;
  content: "";
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.1);
}
.content__left h1 {
  position: absolute;
  top: 50%;
  margin-top: -3vw;
  text-align: center;
  font-family: oswald;
  font-size: 5vw;
  height: 10vw;
  opacity: 1;
  color: #fff;
  width: 100%;
  letter-spacing: 0.15em;
  line-height: 0.6;
}
.content__left span {
  font-size: 1vw;
  font-weight: 300;
  letter-spacing: 0.2em;
  opacity: 0.9;
  font-family: Merriweather;
}
.content__right {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
}
.content__right .content__main {
  position: absolute;
  font-family: Merriweather, serif;
  text-align: left;
  color: #fff;
  font-size: 1.3vw;
  padding: 0 8vw;
  line-height: 1.65;
  font-weight: 300;
  margin: 0;
  opacity: 0.8;
}
.content__right .content__main p:last-child {
  text-transform: uppercase;
  letter-spacing: 0.15em;
  font-size: 0.85em;
}
.content__right .content__index {
  font-size: 30vh;
  position: absolute;
  right: -1vh;
  top: 35vh;
  opacity: 0.04;
  font-family: oswald;
  color: #fff;
}
[data-type="iceland"] .content__left {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/iceland.jpg");
}
.spinner--right [data-type="iceland"] .content__left {
  background-image: none;
}
[data-type="china"] .content__left {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/china.jpg");
}
.spinner--right [data-type="china"] .content__left {
  background-image: none;
}
[data-type="usa"] .content__left {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/usa.jpg");
}
.spinner--right [data-type="usa"] .content__left {
  background-image: none;
}
[data-type="peru"] .content__left {
  background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/215059/peru.jpg");
}
.spinner--right [data-type="peru"] .content__left {
  background-image: none;
}
JS
let activeIndex = 0
let limit = 0
let disabled = false
let $stage
let $controls
let canvas = false

const SPIN_FORWARD_CLASS = 'js-spin-fwd'
const SPIN_BACKWARD_CLASS = 'js-spin-bwd'
const DISABLE_TRANSITIONS_CLASS = 'js-transitions-disabled'
const SPIN_DUR = 1000

const appendControls = () => {
  for (let i = 0; i < limit; i++) {
    $('.carousel__control').append(``)
  }
  let height = $('.carousel__control').children().last().outerHeight()
  
  $('.carousel__control').css('height', (30 + (limit * height)))
  $controls = $('.carousel__control').children()
  $controls.eq(activeIndex).addClass('active')
}

const setIndexes = () => {
    $('.spinner').children().each((i, el) => {
        $(el).attr('data-index', i)
        limit++
    })
}

const duplicateSpinner = () => {
    const $el = $('.spinner').parent()
    const html = $('.spinner').parent().html()
    $el.append(html)
    $('.spinner').last().addClass('spinner--right')
    $('.spinner--right').removeClass('spinner--left')
}

const paintFaces = () => {
    $('.spinner__face').each((i, el) => {
        const $el = $(el)
        let color = $(el).attr('data-bg')
        $el.children().css('backgroundImage', `url(${getBase64PixelByColor(color)})`)
    })
}

const getBase64PixelByColor = (hex) => {
    if (!canvas) {
        canvas = document.createElement('canvas')
        canvas.height = 1
        canvas.width = 1
    }
    if (canvas.getContext) {
        const ctx = canvas.getContext('2d')
        ctx.fillStyle = hex
        ctx.fillRect (0, 0, 1, 1)
        return canvas.toDataURL()
    }
    return false
}

const prepareDom = () => {
    setIndexes()
    paintFaces()
    duplicateSpinner()
    appendControls()
}

const spin = (inc = 1) => {
    if (disabled) return
    if (!inc) return
    activeIndex += inc
    disabled = true

    if (activeIndex >= limit) {
        activeIndex = 0
    }
  
    if (activeIndex < 0) {
        activeIndex = (limit - 1)
    }

    const $activeEls = $('.spinner__face.js-active')
    const $nextEls = $(`.spinner__face[data-index=${activeIndex}]`)
    $nextEls.addClass('js-next')
  
    if (inc > 0) {
      $stage.addClass(SPIN_FORWARD_CLASS)
    } else {
      $stage.addClass(SPIN_BACKWARD_CLASS)
    }
    
    $controls.removeClass('active')
    $controls.eq(activeIndex).addClass('active')
  
    setTimeout(() => {
        spinCallback(inc)
    }, SPIN_DUR, inc)
}

const spinCallback = (inc) => {
    
    $('.js-active').removeClass('js-active')
    $('.js-next').removeClass('js-next').addClass('js-active')
    $stage
        .addClass(DISABLE_TRANSITIONS_CLASS)
        .removeClass(SPIN_FORWARD_CLASS)
        .removeClass(SPIN_BACKWARD_CLASS)
  
    $('.js-active').each((i, el) => {
        const $el = $(el)
        $el.prependTo($el.parent())
    })
    setTimeout(() => {
        $stage.removeClass(DISABLE_TRANSITIONS_CLASS)
        disabled = false
    }, 100)

}

const attachListeners = () => {
  
    document.onkeyup = (e) => {
        switch (e.keyCode) {
            case 38:
                spin(-1)
                break
            case 40:
                spin(1)
                break
            }
    }
 
    $controls.on('click', (e) => {
      e.preventDefault()
      if (disabled) return
      const $el = $(e.target)
      const toIndex = parseInt($el.attr('data-index'), 10)
      spin(toIndex - activeIndex)
      
    })
}

const assignEls = () => {
    $stage = $('.carousel__stage')
}

const init = () => {
    assignEls()
    prepareDom()
    attachListeners()
}


$(() => {
    init();
});
Host Instantly Drag and Drop Website Builder

 

Description

A new spin on the carousel pattern with a split panel transition in three dimensions.
Term
Mon, 11/27/2017 - 22:09

Related Codes

Pen ID
Pen ID
Pen ID