原生JS实现立体金字塔

2020-11-26 15:04:48 浏览数 (1)

采用原生JS及CSS 3.0实现一个金字塔的效果,这个特效在实际工作中用处不大,纯属娱乐。

实现代码如下:

代码语言:javascript复制
<!doctype html>
<html>

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

        #div1 {
            width: 200px;
            height: 200px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin: -100px 0 0 -100px;
            background: black;
            transform-style: preserve-3d;
        }
    </style>
</head>

<body>
    <div id='div1'></div>
    <script type="text/javascript">
        for (var i = 0; i < 100; i  ) {
            var oDiv = document.createElement('div');

            oDiv.style.width = (200 - i * 2)   'px';
            oDiv.style.height = (200 - i * 2)   'px';

            oDiv.style.position = 'absolute';

            oDiv.style.transition = Math.random() * 1   's';
            oDiv.style.left = '0';
            oDiv.style.top = '0';
            oDiv.style.right = '0';
            oDiv.style.bottom = '0';
            oDiv.style.margin = 'auto';

            oDiv.style.transform = 'translateZ('   i   'px)';
            oDiv.style.background = 'rgb('   parseInt(Math.random() * 256)   ','   parseInt(Math.random() * 256)   ','   parseInt(Math.random() * 256)   ')';

            div1.appendChild(oDiv);
        }
        var reg = 0;

        setInterval(function () {
            reg  = 0.4;
            div1.style.transform = 'perspective(800px) rotateX('   reg   'deg)';
        }, 6);

    </script>
</body>

</html>

0 人点赞