首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >原生JS实现立体金字塔

原生JS实现立体金字塔

作者头像
越陌度阡
发布于 2020-11-26 15:04:48
发布于 2020-11-26 15:04:48
2.4K0
举报

采用原生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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/26 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档