zoukankan      html  css  js  c++  java
  • js实现警灯

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>light</title>
    </head>
    <style>
         *{
            margin: 0;
            padding:0
        }
    
        .page{
             100vw;
            height: 100vh;
            overflow: hidden;
            background: #000;
        }
    
        .flex{
            display: flex;
            align-items: center;
            justify-content: center;
        }
    
        .flex-column {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
    
        #openLight{
             100px;
            border-radius:10px;
            background: #fff;
            line-height: 40px;
            text-align: center;
            margin-top:20px;
            cursor: pointer;
        }
    
        .home{
            padding: 30px 50px;
            box-sizing: border-box;
            border: 1px solid #eee;
            box-shadow: 0 2px 100px rgba(0,0,0,0.2);
            background: #fff;
            border-radius: 100px;
            background:transparent
        }
    
        .circle{
             56px;
            height: 56px;
            border-radius: 50%;
            margin: 20px;
            background: #000;
        }
    
        .pink{
            background: red;
            box-shadow: 5px 8px 25px rgba(255, 255, 255, 0.8)
        }
    
        .skyblue{
            background: blue;
            box-shadow: 5px 8px 25px rgba(255, 255, 255, 0.8)
        }
    </style>
    <body>
        <div class="page flex-column">
            <div class="home flex">
              <div class="circle"></div>
              <div class="circle"></div>
              <div class="circle"></div>
              <div class="circle"></div>
              <div class="circle"></div>
              <div class="circle"></div>
              <div class="circle"></div>
              <div class="circle"></div>
            </div>
            <div id="openLight" onclick="openLight()">开灯</div>
        </div>
    </body>
    <script>
        let circles = document.querySelectorAll('.circle')
        let flag = true
        let arr = [
          [1,3,5,7],
          [2,4,6,8],
          [4,5,1,7],
          [5,6,7,8],
          [3,4,5,6],
          [1,2,7,8],
          [3,4,5,6],
          [2,3,5,6]
        ]
        
        // 执行
        function lightMethod(params){
            circles.forEach(function(ele,index){
                if(ele.classList.contains('pink')){
                    ele.classList.remove('pink')
                }
                if(ele.classList.contains('skyblue')){
                    ele.classList.remove('skyblue')
                }
                let className = params.indexOf(index+1)>-1?'skyblue':'pink'
                ele.classList.add(className)
            })
        }
        // 点击开灯
        function openLight(){
            if(flag){
                flag = false
                setTimeout(function(){
                    flag = true
                },900)
                circles.forEach(function(ele,index){
                    ele.classList.remove('black')
                })
                arr.forEach(function(ele,index){
                    setTimeout(function(){
                        lightMethod(ele)
                        // 把灯关了
                        if(index == 7){
                            circles.forEach(function(ele,index){
                                if(ele.classList.contains('pink')){
                                    ele.classList.remove('pink')
                                }
                                if(ele.classList.contains('skyblue')){
                                    ele.classList.remove('skyblue')
                                }
                            })
                        }
                    },index*110)
                })
            }
        }
    </script>
    </html>

    原生JS实现:

    链接:https://pan.baidu.com/s/1gRkRbDfxmVxr20gQeKkmGQ
    提取码:8dze

    vue实现:路由是/light

    链接:https://pan.baidu.com/s/1XDfD3bREhZ6Cc2sGLSxnsA
    提取码:mkg1

  • 相关阅读:
    python基础知识第三篇(列表)
    python基础知识第二篇(字符串)
    python基础知识第一篇(认识Python)
    tomacat环境搭建
    Python的内存管理机制
    selenium定位方法
    python+selenium xpath定位
    django--创建及配置项目app
    django--cookies和session
    django--orm--012
  • 原文地址:https://www.cnblogs.com/wxy0/p/14646591.html
Copyright © 2011-2022 走看看