一个翻翻的小游戏。HTML式。

javascript 复制代码
<!DOCTYPE html>
<html>
<head>
    <style>
    	#grid{
    		width:316px;
    		height: 316px;
    		 background-color: #00BFFF;    		
    	}
        .cell {
            width: 60px;
            height: 60px;
            border: 1px solid black;
            float: left;           
        }
        .red {
            background-color: red;
        }
        .black {
            background-color: black;
        }
    </style>
</head>
<body>
    <div id="grid"></div>
    <script>
        var grid = [];
        var size = 5;
        var gridElement = document.getElementById('grid');

        for (var i = 0; i < size; i++) {
            var row = [];
            for (var j = 0; j < size; j++) {
                var cell = document.createElement('div');
                cell.className = 'cell red';
                cell.onclick = (function(i, j) {
                    return function() {
                        toggleColor(i, j);
                    }
                })(i, j);
                gridElement.appendChild(cell);
                row.push(cell);
            }
            grid.push(row);
        }

        grid[2][2].className = 'cell black';

        function toggleColor(i, j) {
            var directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
            for (var k = 0; k < directions.length; k++) {
                var ni = i + directions[k][0];
                var nj = j + directions[k][1];
                if (ni >= 0 && ni < size && nj >= 0 && nj < size) {
                    var cell = grid[ni][nj];
                    cell.className = cell.className === 'cell red' ? 'cell black' : 'cell red';
                }
            }
        }
    </script>
</body>
</html>
相关推荐
CappuccinoRose6 分钟前
CSS 语法学习文档(十九)
前端·css·属性·flex·grid·学习资源·格式化上下文
雷电法拉珑34 分钟前
财务数据批量采集
linux·前端·python
We་ct1 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:题解与思路解析
前端·算法·leetcode·链表·typescript
前端 贾公子2 小时前
深入理解 Vue3 的 v-model 及自定义指令的实现原理(下)
前端·html
Roc.Chang2 小时前
Vite 启动报错:listen EACCES: permission denied 0.0.0.0:80 解决方案
linux·前端·vue·vite
Desirediscipline2 小时前
cerr << 是C++中用于输出错误信息的标准用法
java·前端·c++·算法
sunny_2 小时前
前端构建产物里的 __esModule 是什么?一次讲清楚它的原理和作用
前端·架构·前端工程化
Soulkey3 小时前
复刻小红书Web端打开详情过渡动画
前端
yuki_uix3 小时前
你点了「保存」之后,数据都经历了什么?
前端