原生JS实现书本立体特效

落爺英雄遲暮 提交于 2019-11-26 22:29:33

给大家分享一个原生JS实现的书本立体特效,效果如下:

实现这个特效需要的三张图片如下:

第一张图:0.jpg

第二张图 1.jpg

第三张图:3.jpg

实现代码如下,欢迎大家复制粘贴。

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>原生JS实现书本立体特效</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        #book {
            width: 290px;
            height: 400px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin: -200px 0 0 -145px;
            background: black;
            transform-style: preserve-3d;

        }

        #book .page {
            position: absolute;
            width: 100%;
            height: 100%;
            background: #ccc;
        }

        #book .topNode {
            position: absolute;
            width: 100%;
            height: 100%;
            background: url(images/2.jpg);
            background-size: 290px 400px;
        }

        #book .bottom {
            position: absolute;
            width: 100%;
            height: 100%;
            background: url(images/1.png);
            background-size: 290px 400px;
        }

        #backNode {
            height: 100%;
            width: 50px;
            background: url(images/0.png);
            position: absolute;
            background-size: 50px 400px;
            /*translateZ为width一半*/
            transform: rotateY(90deg) translateZ(-25px);
        }
    </style>
</head>

<body>
    <div id='book'>
        <div id='backNode'></div>
    </div>
    <script type="text/javascript">

        /*上面的page*/
        for (var i = 0; i < 25; i++) {
            var oPage = document.createElement('div');
            oPage.className = 'page';
            if (i == 24) {
                oPage.className = 'topNode';
            }
            oPage.style.transform = 'translateZ(' + i + 'px)';
            book.appendChild(oPage);
        }

        /*下面的page*/
        for (var i = 0; i < 25; i++) {
            var oPage = document.createElement('div');

            oPage.className = 'page';

            oPage.style.transform = 'translateZ(' + (-i) + 'px)';
            if (i == 24) {
                oPage.className = 'bottom';
                //图片要镜像
                oPage.style.transform = 'translateZ(' + (-i) + 'px) scaleX(-1)';
            }
            book.appendChild(oPage);
        }


        var reg = 0;
        //让书旋转起来
        setInterval(function () {
            reg++;
            book.style.transform = 'perspective(800px) rotateY(' + (reg) + 'deg)';
        }, 4);

    </script>
</body>

</html>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!