zoukankan      html  css  js  c++  java
  • JS实现旋转的魔方

    • js

    <script>
        window.onload = function () {
            let cube = document.querySelector('.cube')
            let timer = null
            let x = 0; y = 0;
            function rotate() {
                cube.style.transform = 'rotateX(' + x + 'deg)' + '' + 'rotateY(' + y + 'deg)';
                x += 30
                y += 45
            }
            function stop() {
                clearInterval(timer)
            }
            clearInterval(timer);
            timer = setInterval(() => {
                rotate();
            }, 1000);
    
           //考虑兼容性
            var hiddenProperty = 'hidden' in document ? 'hidden' :
                'webkitHidden' in document ? 'webkitHidden' :
                    'mozHidden' in document ? 'mozHidden' :
                        null;
            var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
            var onVisibilityChange = function () {
                if (!document[hiddenProperty]) {
                    timer = setInterval(() => {
                        rotate();
                    }, 1000);
                    console.log(hiddenProperty);
                } else {
                    clearInterval(timer)
                }
            }
           //不考虑兼容性
            document.addEventListener(visibilityChangeEvent, onVisibilityChange);
            // document.addEventListener("visibilitychange", function () {
            //     if (!document.hidden) {   //处于当前页面
                         rotate();
            //         timer = setInterval(() => {
            //             rotate();
            //         }, 1000);
            //         console.log('active');
            //     } else {
            //         clearInterval(timer);
            //         console.log('hidden');
            //     }
            // });
        }
    </script>
    
    
    • html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <div class="cube" onclick="stop()">
            <div class="front">front</div>
            <div class="back">back</div>
            <div class="left">left</div>
            <div class="right">right</div>
            <div class="top">top</div>
            <div class="bottom">bottom</div>
        </div>
    </body>
    </html>
    
    
    • css

    
    <style>
        .cube {
             400px;
            height: 400px;
            transition: all 3s;
            margin: 300px auto;
            /* 作用于所有3d转换的子元素 设置给父级 */
            /* perspective: 1000px; */
            /* transform: rotateX(30deg) rotateY(45deg); */
            /* 平面到立方  默认值:平面flat*/
            transform-style: preserve-3d;
    
            position: relative;
        }
        .front,
        .back,
        .left,
        .right,
        .top,
        .bottom {
             100%;
            height: 100%;
            text-align: center;
            line-height: 400px;
            position: absolute;
            opacity: 0.9;
            left: 0;
            top: 0;
        }
        .front {
            background-color: palevioletred;
            background-image: url('http://img1.gtimg.com/tj/pics/hv1/117/208/2153/140051982.jpg');
            transform: translateZ(200px);
        }
    
        .back {
            background-color: yellow;
            transform: translateZ(-200px);
            background-image: url('https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=841408372,3004217725&fm=11&gp=0.jpg');
        }
        .left {
            background-color: pink;
            background-image: url('https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2388632836,3966607624&fm=26&gp=0.jpg');
            transform: rotateY(90deg) translateZ(-200px)
        }
        .right {
            background-color: yellowgreen;
            background-image: url('https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=192610795,467565159&fm=26&gp=0.jpg');
            transform: rotateY(90deg) translateZ(200px)
        }
        .top {
            background-color: skyblue;
            background-image: url("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2936477803,4198185787&fm=15&gp=0.jpg");
            transform: rotateX(90deg) translateZ(200px)
        }
        .bottom {
            background-color: orange;
            background-image: url('http://img0.pclady.com.cn/pclady/1611/02/1612285_jyz.jpg');
            transform: rotateX(90deg) translateZ(-200px)
        }
    </style>
    
    
  • 相关阅读:
    Python(1)-第一天
    读书笔记-单元测试艺术(三)-使用桩对象解除依赖
    Sql2008调试问题
    读书笔记-单元测试艺术(二)-单元测试框架
    读书笔记-单元测试艺术(一)-单元测试的基本知识
    最长回文子串
    最大连续子序列之和,最大连续子序列乘积
    计数排序与位图排序
    筛选法求素数
    传说中的华为面试题(8分钟写出代码)
  • 原文地址:https://www.cnblogs.com/wwj007/p/11251485.html
Copyright © 2011-2022 走看看