• javascript --关灯游戏


    分析:

    搭建10 * 10 的小方块

    <script type="text/javascript" >

      var body = document.getElementsByTagName('body')[0]; //返回数组[0]

      var wrap = document.createElement('div');

      wrap.style.width = '500px';

      wrap.style.height = '500px';

      wrap.style.border = '1px solid gray';

      wrap.style.margin = '0 auto';

      wrap.style.position = 'relative';

      var arr = []; //用于存放每个div小方格

      for(var i = 0; i < 10; i++) {

        for( var j = 0; j < 10; j++) {

          var light = document.createElement('div');  //创建小方格

          light.style.width = '50px';

          light.style.height = '50px';

          light.style.background = 'black';

          light.style.border = '1px solid gray';

          light.style.position = 'absolute';

          light.style.left = i * 10 + '%';

          light.style.top = j * 10 + '%';

          light.index = arr.length;

          arr.push(light);//  将div 添加到数组中

          light.onclick = function() {    //添加点击事件

            var currentLight = event.target;    //获取当前的灯

            if(currentLight.style.background == 'black') {

              currentLight.style.background = 'blue';

            } else {

              currentLight.style.background = 'black';

            }

            if (currentLight.index >= 10) {

              var top = arr[currentLight.index - 10];

              top.style.background = (top.style.background == 'black') ? 'blue' : 'black';

            }

            if (currentLight.index + 10 < arr.length) {

              var bottom = arr[currentLight.index + 10];

              bottom.style.background = (bottom.style.background == 'black') ? 'blue' : 'black';

            }

            if(currentLight.index % 10 != 0 ){

              var left = arr[currentLight.index - 1];

              left.style.background = (left.style.background== 'black') ? 'blue' : 'black';

            }

            if(currentLight.index % 10 < 9) {

              var right = arr[currentLight.index + 1];

              right.style.background = (right.style.background == 'black') ? 'blue' : 'black';

            }

          }

          wrap.appendChild(light); 

        }  

      }

      body.appendChild(wrap); 

    </script>

  • 相关阅读:
    解决com.xpand.. starter-canal 依赖引入问题
    缓存预热加入二级缓存
    缓存预热的实现
    ShardingSphere 中有哪些分布式主键实现方式?
    ShardingSphere 如何实现系统的扩展性
    如何系统剖析 ShardingSphere 的代码结构?
    SharingSphere的数据脱敏
    ShardingSphere的分布式事务
    Qt 事件过滤器原理(installEventFilter函数)
    Qt Event 以及 Event Filter 事件处理
  • 原文地址:https://www.cnblogs.com/zhao12354/p/5734679.html
Copyright © 2020-2023  润新知