cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <main>
    <section class='scene'>
        <div class="cube">
            <div class='box front'>
                <img src="http://coskunbaris.com/codepen/3d-box/batman.jpg" alt=""/>
            </div>
            <div class='box back'>
                <img src="http://coskunbaris.com/codepen/3d-box/superman.jpg" alt=""/>
            </div>
            <div class='box left'>
                <img src="http://coskunbaris.com/codepen/3d-box/spiderman.jpg" alt=""/>
            </div>
            <div class='box right'>
                <img src="http://coskunbaris.com/codepen/3d-box/wolverine.jpg" alt=""/>
            </div>
            <div class='box top'>
                <img src="http://coskunbaris.com/codepen/3d-box/dc-logo.png" alt=""/>
            </div>
            <div class='box bottom'>
                <img src="http://coskunbaris.com/codepen/3d-box/marvel-logo.png" alt=""/>
            </div>
        </div>
    </section>
</main>
            
          
!
            
              $boxWidth: 357px;
$boxHeight: 526px;
$translateZ: $boxWidth / 2;

$supermanColor: #21A3F2;
$spidermanColor: #F22127;
$wolverineColor: #F2CB21;
$batmanColor: #878782;

*, *:before, *:after {
  box-sizing: border-box;
}

body {
  height: 100vh;
  margin: 0;
  transition: all .3s ease-in-out;
  @extend .batman;

  &.batman {
    background: $batmanColor;
  }
  &.superman {
    background: $supermanColor;
  }
  &.spiderman {
    background: $spidermanColor;
  }
  &.wolverine {
    background: $wolverineColor;
  }
}

main {
  display: inline-block;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.scene {
  perspective: 1000px;
  position: relative;
  width: $boxWidth;
  height: $boxHeight;
}

.cube {
  width: 100%;
  height: 100%;
  position: absolute;
  transform-style: preserve-3d;
  transform: translateZ(-#{$translateZ});
  user-select: none;

  img {
    pointer-events: none;
  }
}

.box {
  display: block;
  position: absolute;
  margin: 0;

  &.front {
    transform: rotateY(0deg) translateZ($translateZ);
    width: $boxWidth;
    height: $boxHeight;
  }
  &.back {
    transform: rotateY(180deg) translateZ($translateZ);
    width: $boxWidth;
    height: $boxHeight;
  }
  &.left {
    transform: rotateY(-90deg) translateZ($translateZ);
    width: $boxWidth;
    height: $boxHeight;
  }
  &.right {
    transform: rotateY(90deg) translateZ($translateZ);
    width: $boxWidth;
    height: $boxHeight;
  }
  &.top {
    transform: rotateX(90deg) translateZ($translateZ);
    width: $boxWidth;
    height: $boxWidth;
  }
  &.bottom {
    transform: rotateX(-90deg) translateZ(calc(#{$boxWidth} - 10px));
    width: $boxWidth;
    height: $boxWidth;
  }

  &.front, &.back, &.left, &.right {
    img {
      width: 100%;
      height: 100%;
    }
  }
  &.top, &.bottom {
    background: #fff;

    img {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
    }
  }
}
            
          
!
            
              /**
 * Coskun Baris, coskunbaris.com
 */

var cube = {
    selector : $('.cube'),
    touchSelector: document.querySelector('.cube'),
    scene: $('.scene'),
    rotateX : 0,
    rotateY : 0,
    delay : .3,
    ease: Elastic.easeNone,
    sensation: 5,
    toLeft : function (distance) {
        TweenMax.to(this.selector, this.delay, {
            rotationY: '-=' + distance/cube.sensation,
            ease: cube.ease,
            onComplete : function () {
                cube.updateRotate();
                bgListener();
            }
        });
    },
    toRight : function (distance) {
        TweenMax.to(this.selector, this.delay, {
            rotationY: '+=' + distance/cube.sensation,
            ease: cube.ease,
            onComplete : function () {
                cube.updateRotate();
                bgListener();
            }
        });
    },
    toUp : function (distance) {
        TweenMax.to(this.selector, this.delay, {
            rotationX: '+=' + distance/cube.sensation,
            ease: cube.ease,
            onComplete : function () {
                cube.updateRotate();
                bgListener();
            }
        });
    },
    toDown : function (distance) {
        TweenMax.to(this.selector, this.delay, {
            rotationX: '-=' + distance/cube.sensation,
            ease: cube.ease,
            onComplete : function () {
                cube.updateRotate();
                bgListener();
            }
        });
    },
    updateRotate : function () {

        if (cube.selector["0"]._gsTransform.rotationY <= -360) {
            this.selector["0"]._gsTransform.rotationY += 360;
            this.rotateX = cube.selector["0"]._gsTransform.rotationX;
            this.rotateY = cube.selector["0"]._gsTransform.rotationY;
        }
        if (cube.selector["0"]._gsTransform.rotationY >= 360) {
            this.selector["0"]._gsTransform.rotationY -= 360;
            this.rotateX = cube.selector["0"]._gsTransform.rotationX;
            this.rotateY = cube.selector["0"]._gsTransform.rotationY;
        }
        else {
            this.rotateX = cube.selector["0"]._gsTransform.rotationX;
            this.rotateY = cube.selector["0"]._gsTransform.rotationY;
        }

    }
};
var swiper = {
    directionX: null,
    directionY: null,
    firstX: null,
    lastX: null,
    firstY: null,
    lastY: null,
    distanceX: null,
    distanceY: null
};

/*SWIPER*/
/*----------------------------------------*/
function firstPosition(event) {
    swiper.firstX = event.pageX;
    swiper.firstY = event.pageY;
}
function lastPosition(event) {
    swiper.lastX = event.pageX;
    swiper.lastY = event.pageY;
}
function directionControl(){
    swiper.firstX > swiper.lastX ? swiper.directionX = 'left' : swiper.directionX = 'right';
    swiper.firstY > swiper.lastY ? swiper.directionY = 'up' : swiper.directionY = 'down';
}
function distanceControl(){
    swiper.directionX == 'right' ? swiper.distanceX = swiper.lastX - swiper.firstX : swiper.distanceX = swiper.firstX - swiper.lastX;
    swiper.directionY == 'down' ? swiper.distanceY = swiper.lastY - swiper.firstY : swiper.distanceY = swiper.firstY - swiper.lastY;
}

/*BACKGROUND*/
/*----------------------------------------*/
function bgListener() {
    var frontQuery = (cube.rotateY > -45 && cube.rotateY < 45);
    var backQuery = ( ( cube.rotateY > -225 && cube.rotateY < -135 ) || ( cube.rotateY > 135 && cube.rotateY < 225 ) );
    var rightQuery = ( ( cube.rotateY > -135 && cube.rotateY < -45 ) || ( cube.rotateY > 225 && cube.rotateY < 315 ) );
    var leftQuery = ( ( cube.rotateY > -315 && cube.rotateY < -225 ) || ( cube.rotateY > 45 && cube.rotateY < 135 ) );

    if(frontQuery) {
        $('body').addClass('batman').removeClass('spiderman wolverine superman');
    }
    else if(backQuery) {
        $('body').addClass('superman').removeClass('spiderman wolverine batman');
    }
    else if(rightQuery) {
        $('body').addClass('wolverine').removeClass('spiderman superman batman');
    }
    else if(leftQuery) {
        $('body').addClass('spiderman').removeClass('wolverine superman batman');
    }
}

/*CUBE ROTATE*/
/*----------------------------------------*/
function cubeRotate() {
    if (swiper.directionX == 'left') {
        cube.toLeft(swiper.distanceX);
    }
    else if (swiper.directionX == 'right') {
        cube.toRight(swiper.distanceX);
    }

    if (swiper.directionY == 'up') {
        if (cube.rotateY >= 0 && cube.rotateY < 90) {
            cube.toUp(swiper.distanceY);
        }
        else if (cube.rotateY >= 90 && cube.rotateY < 180) {
            cube.toDown(swiper.distanceY);
        }
        else if (cube.rotateY >= 180 && cube.rotateY < 270) {
            cube.toDown(swiper.distanceY);
        }
        else if (cube.rotateY >= 270 && cube.rotateY < 360) {
            cube.toUp(swiper.distanceY);
        }

        else if (cube.rotateY <= 0 && cube.rotateY > (-90)) {
            cube.toUp(swiper.distanceY);
        }
        else if (cube.rotateY <= (-90) && cube.rotateY > (-180)) {
            cube.toDown(swiper.distanceY);
        }
        else if (cube.rotateY <= (-180) && cube.rotateY > (-270)) {
            cube.toDown(swiper.distanceY);
        }
        else if (cube.rotateY <= (-270) && cube.rotateY > (-360)) {
            cube.toUp(swiper.distanceY);
        }
    }
    else if (swiper.directionY == 'down') {
        if (cube.rotateY >= 0 && cube.rotateY < 90) {
            cube.toDown(swiper.distanceY);
        }
        else if (cube.rotateY >= 90 && cube.rotateY < 180) {
            cube.toUp(swiper.distanceY);
        }
        else if (cube.rotateY >= 180 && cube.rotateY < 270) {
            cube.toUp(swiper.distanceY);
        }
        else if (cube.rotateY >= 270 && cube.rotateY < 360) {
            cube.toDown(swiper.distanceY);
        }

        else if (cube.rotateY <= 0 && cube.rotateY > (-90)) {
            cube.toDown(swiper.distanceY);
        }
        else if (cube.rotateY <= (-90) && cube.rotateY > (-180)) {
            cube.toUp(swiper.distanceY);
        }
        else if (cube.rotateY <= (-180) && cube.rotateY > (-270)) {
            cube.toUp(swiper.distanceY);
        }
        else if (cube.rotateY <= (-270) && cube.rotateY > (-360)) {
            cube.toDown(swiper.distanceY);
        }
    }
}
/*----------------------------------------*/

$(function () {
    "use strict";

    var touch, onDrag;

    cube.selector.on('mousedown', function(event){
        onDrag = true;

        firstPosition(event);

        cube.selector.on('mousemove', function (event) {
            if(onDrag === true) {
                lastPosition(event);
                directionControl();
                distanceControl();

                cubeRotate();
            }
        });
    });
    cube.selector.bind('mouseup mouseover mouseout', function () {
        onDrag = false;
    });

    cube.touchSelector.addEventListener('touchstart', function(event) {
        onDrag = true;
        touch = event.touches[0];
        firstPosition(touch);

        cube.touchSelector.addEventListener('touchmove', function (event) {
            if(onDrag === true) {
                touch = event.changedTouches["0"];

                lastPosition(touch);
                directionControl();
                distanceControl();
                cubeRotate();
            }
        });
    });
    cube.touchSelector.addEventListener('touchend', function(event) {
        onDrag = false;
    });
});
            
          
!
999px
Loading ..................

Console