Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header class="header container">
    <h1 class="pen__heading">3D Parallax Boxes</h1>
    <h4 class="pen__subheading">By: <a href="http://kylebrumm.com" target="_blank">Kyle Brumm</a></h4>
</header>

<main class="main container">
    <div class="box-wrap"
         data-parallax="0.05"
         data-rotate-x="50"
         data-rotate-x-speed="0.5"
         data-rotate-y="10"
         data-rotate-y-speed="-0.1">
        <div class="box box--dark">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.1"
         data-rotate-x="70"
         data-rotate-x-speed="0.4"
         data-rotate-y="40"
         data-rotate-y-speed="0.4">
        <div class="box">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="1"
         data-rotate-x="10"
         data-rotate-x-speed="0.1"
         data-rotate-y="80"
         data-rotate-y-speed="0.8">
        <div class="box">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.6"
         data-rotate-x="20"
         data-rotate-x-speed="0.2"
         data-rotate-y="20"
         data-rotate-y-speed="0.4">
        <div class="box box--light">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.1"
         data-rotate-x="80"
         data-rotate-x-speed="0.8"
         data-rotate-y="45"
         data-rotate-y-speed="-0.6">
        <div class="box box--dark">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.7"
         data-rotate-x="85"
         data-rotate-x-speed="0.2"
         data-rotate-y="15"
         data-rotate-y-speed="0.65">
        <div class="box box--light">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.1"
         data-rotate-x="70"
         data-rotate-x-speed="0.9"
         data-rotate-y="45"
         data-rotate-y-speed="0.9">
        <div class="box box--dark">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.4"
         data-rotate-x="55"
         data-rotate-x-speed="1"
         data-rotate-y="55"
         data-rotate-y-speed="0.6">
        <div class="box box--light">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.8"
         data-rotate-x="35"
         data-rotate-x-speed="0.3"
         data-rotate-y="45"
         data-rotate-y-speed="0.3">
        <div class="box">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>

    <div class="box-wrap"
         data-parallax="0.1"
         data-rotate-x="15"
         data-rotate-x-speed="0.9"
         data-rotate-y="65"
         data-rotate-y-speed="0.3">
        <div class="box">
            <div class="side front"></div>
            <div class="side back"></div>
            <div class="side right"></div>
            <div class="side left"></div>
            <div class="side top"></div>
            <div class="side bottom"></div>
        </div>
    </div>
</main>
              
            
!

CSS

              
                @function random-color($min: 0, $max: 255, $alpha: 1, $red: null, $green: null, $blue: null) {
    @if $min < 0 {
        $min: -1;
    } @else {
        $min: $min - 1;
    }
    @if $max > 255 {
        $max: 256;
    } @else {
        $max: $max + 1;
    }

    $range: $max - $min;
    @if not $red { $red: random($range) + $min; }
    @if not $green { $green: random($range) + $min; }
    @if not $blue { $blue: random($range) + $min; }

    @return rgba($red, $green, $blue, $alpha);
}

// Colors
$primary: random-color($min: 100, $max:200);
$primary-light: lighten($primary, 15%);
$primary-dark: darken($primary, 15%);
$primary-darker: darken($primary, 25%);
$black: #333333;
$white: #eeeeee;

// Fonts
@import url(https://fonts.googleapis.com/css?family=Open+Sans:400|Raleway:300);
$open-sans: 'Open Sans', Helvetica, arial, sans-serif;
$raleway: 'Raleway', 'Open Sans', sans-serif;

// Misc
$max-width: 800px;


// ------------------------------------------------------


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

html {
    font-family: $open-sans;
    color: $black;
    background-color: $white;
}

h1, h2, h3,
h4, h5, h6 {
    font-family: $raleway;
    text-align: center;
}

a {
    color: $black;
    text-decoration: none;
}

img {
    max-width: 100%;
}

.header {
    position: fixed;
    overflow: visible;
    width: 100%;
    max-width: none !important;
    
    &:after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        width: 50px;
        height: 2px;
        background-color: $primary;
        transform: translateX(-50%);
    }
}

.pen__heading {}

.pen__subheading {
    margin-bottom: 0;
    
    a {
        color: $primary;
        
        &:hover,
        &:focus {
            color: $primary-light;
        }
    }
}

.container {
    overflow: hidden;
    width: 100%;
    max-width: $max-width;
    margin: 0 auto;
    padding: 2rem 1rem;
}

.col {
    padding: 1rem;
    
    @media (min-width: $max-width) {
        width: 50%;
        float: left;

        &:nth-of-type(2n+1) {
            clear: left;
        }        
    }
    
    &.col--full {
        width: 100%;
    }
}


// ------------------------------------------------------


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

$width: 250px;
$height: 250px;
$depth: 250px;

.box-wrap {
    position: relative;
    display: block;
    width: $width;
    height: $height;
    perspective: 2000px;
}

.box {
    position: absolute;
    width: 100%;
    height: 100%;
    transform-style: preserve-3d;
    backface-visibility: hidden;
    transform: rotateX(25deg)
               rotateY(25deg);

    .side {
        margin: 0;
        display: block;
        position: absolute;
        background-size: cover;
        background-position: center;
        backface-visibility: hidden;
        border: 2px solid $primary-darker;
        background-color: $primary;
    }

    .front,
    .back {
        width: $width + 1px;
        height: $height + 1px;
    }

    .right,
    .left {
        width: $depth + 1px;
        height: $height + 1px;
        left: (($width - $depth) / 2);
    }

    .top,
    .bottom {
        width: $width + 1px;
        height: $depth + 1px;
        top: (($height - $depth) / 2);
    }
    
    .front { transform: rotateY(0deg) translateZ($depth / 2); }
    .back { transform: rotateY(180deg) translateZ($depth / 2); }
    .right { transform: rotateY(90deg) translateZ($width / 2); }
    .left { transform: rotateY(-90deg) translateZ($width / 2); }
    .top { transform: rotateX(90deg) translateZ($height / 2); }
    .bottom { transform: rotateX(-90deg) translateZ($height / 2); }
}


// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//


body {
    height: 250vh;
}

.box--light {
    .side {
        background-color: $primary-light;
    }
}

.box--dark {
    .side {
        background-color: $primary-dark;
    }
}

.box-wrap {
    position: absolute;
    
    &:nth-of-type(1) {
        top: 80vh;
        left: 0vw;
    }
    
    &:nth-of-type(2) {
        top: 80vh;
        left: 34vw;
    }
    
    &:nth-of-type(3) {
        top: 80vh;
        right: 10vw;
    }
    
    &:nth-of-type(4) {
        top: 60vh;
        right: 30vw;
    }
    
    &:nth-of-type(5) {
        top: 75vh;
        right: 0vw;
    }
    
    &:nth-of-type(6) {
        top: 90vh;
        left: 5vw;
    }
    
    &:nth-of-type(7) {
        top: 110vh;
        left: 35vw;
    }
    
    &:nth-of-type(8) {
        top: 100vh;
        right: 10vw;
    }
    
    &:nth-of-type(9) {
        top: 70vh;
        left: 20vw;
    }
    
    &:nth-of-type(10) {
        top: 95vh;
        right: 25vw;
    }
}
              
            
!

JS

              
                (function($) {
    var threeD = {
        // Initialize everything
        init: function() {
            this.rotatingBoxes();
        },

        // Rotate some boxes
        rotatingBoxes: function() {
            var $rotate_els = $('.box-wrap');

            function updateElementState() {
                $rotate_els.each(function(idx) {
                    // Parallax
                    var speed = this.dataset.parallax || 0.25;
                    var offset = (-(window.pageYOffset - this.offsetTop) * speed);
                    this.style.transform = 'translate3d(0, ' + offset + 'px, 0)';
                    this.style.OTransform = 'translate3d(0, ' + offset + 'px, 0)';
                    this.style.msTransform = 'translate3d(0, ' + offset + 'px, 0)';
                    this.style.MozTransform = 'translate3d(0, ' + offset + 'px, 0)';
                    this.style.WebkitTransform = 'translate3d(0, ' + offset + 'px, 0)';
                   
                    var pageOffset = window.pageYOffset;
                    
                    // X Rotation
                    var initialRotationX = $(this).data('rotate-x') || 25;
                    var rotationSpeedX = $(this).data('rotate-x-speed') || 0;
                    var rotationX = (rotationSpeedX == 0) ? 0 : (-(pageOffset / (rotationSpeedX * 100)) * 10 / 2);
                    var rotateX = rotationX + initialRotationX;
                    
                    // Y Rotation
                    var initialRotationY = $(this).data('rotate-y') || 25;
                    var rotationSpeedY = $(this).data('rotate-y-speed') || 0;
                    var rotationY = (rotationSpeedY == 0) ? 0 : (-(pageOffset / (rotationSpeedY * 100)) * 10 / 2);
                    var rotateY = rotationY + initialRotationY;
                    
                    // Rotate
                    var $child = $(this).find('.box');
                    $child.css({
                        '-webkit-transform' : 'rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)',
                        '-moz-transform'    : 'rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)',
                        '-ms-transform'     : 'rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)',
                        '-o-transform'      : 'rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)',
                        'transform'         : 'rotateX(' + rotateX + 'deg) rotateY(' + rotateY + 'deg)'
                    });
                });
            }

            updateElementState();

            $(window).scroll(function() {
                updateElementState();
            });
        }
    }

    // Run it
    threeD.init();
})(jQuery);
              
            
!
999px

Console