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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!-- Wrapper for all the content -->
<div class="box3d-wrapper">
    <!-- Paralelipiped wrapper -->
  	<div class="box3d-container">
        <!-- Paralelipiped face container -->
        <div class="boxFace">
            <span>Story_1</span> 
        </div>
        <!-- Paralelipiped face container -->
        <div class="boxFace">
            <span>Story_2</span> 
        </div>
        <!-- Paralelipiped face container -->
        <div class="boxFace">
            <span>Story_3</span> 
        </div>
        <!-- Paralelipiped face container -->
        <div class="boxFace">
            <span>Story_4</span> 
        </div>
    </div>
</div>
            
          
!
            
              /* Style the parent */
.box3d-wrapper {
    background-image: -webkit-radial-gradient(circle, #555, #333 90%);
    background-image:      -o-radial-gradient(circle, #555, #333 90%);
    background-image:         radial-gradient(circle, #555, #333 90%);
    height: 100vh;
    left: 0;
    overflow-x: hidden;
    position: fixed;
    top: 0;
    -webkit-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
            user-select: none;
    width: 100vw;

    -webkit-perspective: 200vw;
            perspective: 200vw;
    -webkit-perspective-origin: 50% 50%;
            perspective-origin: 50% 50%;
}

/* Faces Container */
.box3d-container {
    width: 100%;
    height: 100%;
}

.box3d-container {
    height: 100%;
    margin: 0;
    opacity: 1;
    position: relative !important;
    -webkit-transform-origin: 50% 50% -50vw;
        -ms-transform-origin: 50% 50% -50vw;
            transform-origin: 50% 50% -50vw;
    -webkit-transition: all .3s ease;
            transition: all .3s ease;
    width: 100%;

    -webkit-transform-style: preserve-3d;
            transform-style: preserve-3d;
}

/* Remove css animation - This will happen during the rotation */
.box3d-container.removeAnimation {
    -webkit-transition: none;
         -o-transition: none;
            transition: none;
}

/* Hide the block - This class will be removed when we've all the items in the view */
.box3d-container.hide {
    opacity: 0;
    pointer-events: none;
}

/* Remove the Script tag if we've any */
.box3d-container script {
    display: none !important;
}

/* Style the Faces */
.box3d-container .boxFace {
    background-color: #333;
    background-position: center center;
    background-size: cover;
    border: 1px solid #000;
    -webkit-box-sizing: border-box;
            box-sizing: border-box;
    height: inherit;
    position: absolute;
    -webkit-transform-origin: 0 0;
        -ms-transform-origin: 0 0;
            transform-origin: 0 0;
    width: inherit;
    z-index: 0;

    -webkit-transform-style: preserve-3d;
            transform-style: preserve-3d;
}

/* Put the first face on top of all other faces */
.box3d-container .boxFace:first-child {
    z-index: 1;
}

/* Style the span that are inside of each box */
.box3d-container .boxFace span {
    background: rgba(255, 255, 255, .8);
    border-radius: 100%;
    -webkit-box-shadow: 0 0 40vw 10px #3e3e3e;
            box-shadow: 0 0 40vw 10px #3e3e3e;
    display: block;
    font-size: 22px;
    height: 40vw;
    left: 50%;
    line-height: 40vw;
    position: absolute;
    text-align: center;
    top: 50%;
    -webkit-transform: translateX(-50%) translateY(-50%);
        -ms-transform: translateX(-50%) translateY(-50%);
            transform: translateX(-50%) translateY(-50%);
    width: 40vw;
}

/* Style for the first face */
.boxFace1 {
    background-image: url("https://images.unsplash.com/photo-1520507341732-da9d37be9072?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=207baa099c642a67816d3aa602c9d846&auto=format&fit=crop&w=1050&q=80");
}

/* Style for the second face */
.boxFace2 {
    background-image: url("https://images.unsplash.com/photo-1523761682445-cab4588183dd?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5254c3ca6840a30cf3d481f2f5c3f5a1&auto=format&fit=crop&w=1050&q=80");
    left: 100%;
    -webkit-transform: rotateY(90deg) translateZ(0);
            transform: rotateY(90deg) translateZ(0);
}

/* Style for the third face */
.boxFace3 {
    background-image: url("https://images.unsplash.com/photo-1532696059848-96604f3237f0?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a61a7b07f1cc381ccab1a8592b63ecb6&auto=format&fit=crop&w=967&q=80");
    -webkit-transform: translateX(100vw) translateZ(-100vw) scaleX(-1);
            transform: translateX(100vw) translateZ(-100vw) scaleX(-1);
}

/* Style for the fourth face */
.boxFace4 {
    background-image: url("https://images.unsplash.com/photo-1464740042629-b78a46b310ae?ixlib=rb-0.3.5&s=be05ddba8c3cf320b0c395eb45b3e9e8&auto=format&fit=crop&w=1050&q=80");
    -webkit-transform: rotateY(-90deg) translateX(-100vw);
            transform: rotateY(-90deg) translateX(-100vw);
}
            
          
!
            
              window.OS_Story = function() {
    // properties
    var _this = this;

    _this._defaultOptions = {
        elementId: ".box3d-container",
        totalFaces: 1,
        callBack: function(){
            return null;
        }
    };

    _this.htmlElement, _this.startTouchX, _this.UpdateTouchX, 
    _this.EndTouchX, _this.draggedDist, _this.sideMoving,
    _this.viewportWidth = 0,
    _this.rotateNumber = 0,
    _this.scaleValue = 1,
    _this.rotateYValue = 0,
    _this.prevBoxFace = _this.activeBoxFace = 1,
    _this.isMoving = false;
    
    // methods
    var
        // auxiliar function that will remove all the content - destroy
        destroy = function() {
            // remove the associated events
            addOrRemoveEvents(false);
            // remove all the object stucture
            _this.htmlElement.parentNode.remove();
            // reset the object
            _this = {
                "removed": true
            };
            return _this;
        },

        // auxiliar function to add or remove events
        addOrRemoveEvents = function(addEvents) {
            if (addEvents) {
                _this.htmlElement.addEventListener("touchstart", startRotation, false);
                _this.htmlElement.addEventListener("touchmove", updateAnimation, false);
                _this.htmlElement.addEventListener("touchend", stopRotation, false);
            } else {
                _this.htmlElement.removeEventListener("touchstart", startRotation);
                _this.htmlElement.removeEventListener("touchmove", updateAnimation);
                _this.htmlElement.removeEventListener("touchend", stopRotation);
            }
        },

        // auxiliar function that will check witch face will be changed with new content
        updateFaceContent = function(updateFace, newFaceIndex) {
            // update the faceBox atributes that will be changed
            var faceBoxToUpdate = document.querySelectorAll("[facenumber='"+updateFace+"']")[0];
            faceBoxToUpdate.setAttribute("facenumber", newFaceIndex);

            // get the number of faceNumber that will be updated/changed
            var windowNumberUpdate = parseInt(faceBoxToUpdate.getAttribute("windownumber"));

            // call the callBack function that will perform the changes in the face that will be updated/changed
            if(_this._defaultOptions.callBack && _this._defaultOptions.callBack != null) {
                _this._defaultOptions.callBack(updateFace, newFaceIndex,  windowNumberUpdate);
            }
        },

        // auxiliar function to add events when the animation is end
        endOfAnimation = function() {
            addOrRemoveEvents(true);

            if (_this.activeBoxFace != _this.prevBoxFace) {
                
                // rotate to left => show the next image
                if(_this.sideMoving === "-" && _this.activeBoxFace >= 3 && _this.activeBoxFace <= _this._defaultOptions.totalFaces-2) {
                    updateFaceContent(_this.activeBoxFace-2, _this.activeBoxFace+2);
                }

                // rotate to right => show the prev image
                if(_this.sideMoving === "+" && _this.activeBoxFace >= 2 && _this.activeBoxFace <= _this._defaultOptions.totalFaces-3) {
                    updateFaceContent(_this.activeBoxFace+3, _this.activeBoxFace-1);
                }
                
                // update the new activeBoxFace
                _this.prevBoxFace = _this.activeBoxFace;
            }
        },

        // auxiliar function that returns the degree value related to the "drag" distance
        calculateDegreesValue = function(draggedDistValue) {
            var pixelDegreeValue = 90 / _this.viewportWidth;
            return draggedDistValue * pixelDegreeValue;
        }

        // auxiliar function that returns the scale value in the same proportion of rotation
        calculateScaleRotation = function(rotationValue) {
            return 0.9 + Math.abs((((Math.abs(rotationValue) % 90)) - 45) / 45) * 0.1;
        },

        // auxiliar function that will apply the rotation and scale style to the element
        applyTheRotateAndScaleValues = function() {
            _this.htmlElement.style.transform = "scale(" + _this.scaleValue + ") rotateY(" + _this.rotateYValue + "deg)";
            _this.htmlElement.style.webkitTransform = "scale(" + _this.scaleValue + ") rotateY(" + _this.rotateYValue + "deg)";

            if (_this.isMoving) {
                window.requestAnimationFrame(applyTheRotateAndScaleValues);
            }
        },

        // perform the rotation stop
        stopRotation = function(e) {
            _this.isMoving = false;
            
            // remove the events applied if we are on the first face and if we try to go back
            if (_this.sideMoving !== "+" && _this.activeBoxFace !== 1 && 
                _this.sideMoving !== "-" && _this.activeBoxFace !== _this._defaultOptions.totalFaces) {
                addOrRemoveEvents(false);
            }

            // remove the class that removes the css animation
            _this.htmlElement.classList.remove("removeAnimation");

            // get the last X coordenate when user stop the touch
            _this.EndTouchX = e.changedTouches[0].clientX;

            // check if user has dragged a distance greater than the half of viewport size
            if (Math.abs(_this.draggedDist) > _this.viewportWidth / 2) {
                // check if we are rotating to the rigth
                if (_this.sideMoving === "+") {
                    _this.activeBoxFace--;
                    _this.rotateNumber++;
                }

                // check if we are rotating to the left
                if (_this.sideMoving === "-") {
                    _this.activeBoxFace++;
                    _this.rotateNumber--;
                }
            }

            _this.scaleValue = 1;
            _this.rotateYValue = _this.rotateNumber * 90;
            applyTheRotateAndScaleValues();

            // reset calc values;
            _this.draggedDist = 0;
        },

        // update animation status
        updateAnimation = function(e) {
            // update touch X position
            _this.UpdateTouchX = e.changedTouches[0].clientX;

            // see what the direction user is taken
            if (_this.UpdateTouchX > _this.startTouchX) {
                _this.sideMoving = "+";
            } else {
                _this.sideMoving = "-";
            }

            // block left animation when the BoxFace it's the first one
            if (_this.sideMoving === "+" && _this.activeBoxFace === 1 || 
                _this.sideMoving === "-" && _this.activeBoxFace === _this._defaultOptions.totalFaces){
                return false;
            }

            // get the "drag" distance value
            _this.draggedDist = _this.UpdateTouchX - _this.startTouchX;

            // get the rotation degrees value per "drag" distance
            _this.rotateYValue = _this.rotateNumber * 90 + calculateDegreesValue(_this.draggedDist);

            // apply the rotation and scale to the element
            _this.scaleValue = calculateScaleRotation(_this.rotateYValue);
            window.requestAnimationFrame(applyTheRotateAndScaleValues);
        },

        // Perform that rotation start
        startRotation = function(e) {
            _this.isMoving = true;
            // Add the class that will be remove the css animation
            _this.htmlElement.classList.add("removeAnimation");
            // save the X coordenate when user touch the screen
            _this.startTouchX = e.changedTouches[0].clientX;
        },

        // init function
        init = function(opts) {

            // update defaultOptions values
            for (var i = 0; i < Object.keys(opts).length; i++) {
                if (_this._defaultOptions[Object.keys(opts)[i]]) {
                    _this._defaultOptions[Object.keys(opts)[i]] = opts[Object.keys(opts)[i]];
                }
            }
            
            console.log(_this);

            // get all the boxFaces html elements
            var facesInView;
            // fallback in case user doesn't pass the id for our element
            if (_this._defaultOptions.elementId[0] !== ".") {
                facesInView = document.querySelectorAll("#"+_this._defaultOptions.elementId + " .boxFace");
            }
            else{
                facesInView = document.querySelectorAll(_this._defaultOptions.elementId + " .boxFace");
            }

            // update classes and attributes to the 4 child faceBox elements
            for (var j = 0; j < facesInView.length; j++) {
                // admit only the first 4 elements (if the developer added more than 4 in the view)
                if(j < 4){
                    var className = "boxFace"+(j+1);
                    facesInView[j].setAttribute("class", "");
                    facesInView[j].classList.add("boxFace", className);
                    facesInView[j].setAttribute("facenumber", j+1);
                    facesInView[j].setAttribute("windownumber", j+1);
                }
                else{
                    // remove all the next items that could be have
                    facesInView[j].remove();
                }
            }

            // get the element that will be our rotated Box
            if (_this._defaultOptions.elementId[0] !== ".") {
                _this.htmlElement = document.getElementById(_this._defaultOptions.elementId);
            }
            else{
                // in case user doesn't pass the id for our element
                _this.htmlElement = document.querySelectorAll(_this._defaultOptions.elementId)[0];
            }
            
            // Clean the block initial structure and add just the boxFace elements
            _this.htmlElement.innerHTML = "";
            for (var k = 0; k < 4; k++) {
                _this.htmlElement.appendChild(facesInView[k]);
            }

            // add the event that will be dispached when the transition animation ends
            _this.htmlElement.addEventListener("transitionend", endOfAnimation);

            // auxiliar function that will add or remove the events to our box
            addOrRemoveEvents(true);

            // make the content appear
            _this.htmlElement.classList.remove("hide");
        };

    return {
        destroy: function(){
            return destroy();
        },
        setViewportWidth: function(val) {
            _this.viewportWidth = val;
        },
        init: function(options) {
            init(options);
        }
    };
}

// function that will be responsible to update the content of the updated/changed Face
function callBackFunction(updateFace, newFaceIndex,  windowNumberUpdate){
    var faceToUpdate = document.querySelectorAll("[facenumber='"+newFaceIndex+"']")[0],
        faceToUpdate_children = faceToUpdate.children[0];

    faceToUpdate_children.innerHTML = "Story_"+newFaceIndex;
}

// Wait the window completed loaded all info
window.onload = function() {
    // Create a new instance of the OS_Storie Object
    window.OS_StorieItem = new window.OS_Story();
    // Pass the viewport width to the Object
    window.OS_StorieItem.setViewportWidth(document.documentElement.clientWidth);
    // Execute the Object init function with the info that will be needed
    window.OS_StorieItem.init({
        elementId: ".box3d-container",
        totalFaces: 5,
        callBack: callBackFunction
    });
};

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console