原生Js实现踩白块

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./demo.css">
</head>
<body>
    <div class="wrapper">
        <div id="go">
            <a href="#">Game</a>
        </div>
        <div id="main"></div>
    </div>
    <script src="./index.js"></script>
</body>
</html>
*{
    padding: 0;
    margin: 0;
    text-decoration: none;
}
.wrapper{
    position: relative;
    width: 400px;
    height: 600px;
    margin: 150px auto;
    /* border: 1px solid #000; */
    overflow: hidden;
}
.wrapper #go{
    width: 100%;
    position: absolute;
    top: 0;
    text-align: center;
    z-index: 95;
}
.wrapper #go a{
    font-size: 60px;
    color: cyan;
}

.wrapper #main{
    width: 100%;
    height: 100%;
    /* border: 1px solid red; */
    position: relative;
    top: -150px;
}
.row{
    width: 100%;
    height: 150px;
}
.row div{
    float: left;
    width: 99px;
    /* border-left: 1px solid ; #000*/
    height: 149px;
    /* border-bottom: 1px solid ; #000*/
}
var go = document.getElementById('go'); //背景dom
var main = document.getElementById('main'); //运动dom
var colors = ['red', 'yellow', 'blue', 'cyan']; //随机颜色数组
var speed = 5, //加速度
    num = 0, //计数器
    timer; //计时器
flag = true; //结束后加锁


function clickStart() { //开始函数
    go.addEventListener('click', function () {
        go.style.display = 'none'; //隐藏开始元素
        move();
    })
}
clickStart();

//运动  判断
function move() {
    clearInterval(timer); //清除
    timer = setInterval(function () {
        var step = parseInt(main.offsetTop) + speed; //运动速度
        main.style.top = step + 'px';
        if (parseInt(main.offsetTop) >= 0) { //判断跳转回原位
            main.style.top = '-150px';
            cDiv(); //dom创建
        }
        var len = main.childNodes.length; //元素个数
        if (len == 6) {
            for (let i = 0; i < 4; i++) { //判断class属性结束
                if (main.childNodes[len - 1].childNodes[i].classList.container('i')) {
                    alert('结束 ,得分:' + num); //累加得分
                    clearInterval(timer); //清除
                    flag = false; //点击锁

                }

            }
            main.removeChild(main.childNodes[len - 1]) //元素个数到了6就删除下元素
        }
    }, 20);
    binEvent(); //点击判断函数

}
//创建dom
function cDiv() {
    var oDiv = document.createElement('div');
    var index = Math.floor(Math.random() * 4); //随机颜色
    oDiv.setAttribute('class', 'row'); //添加样式
    for (let j = 0; j < 4; j++) { //单排元素添加
        var idiv = document.createElement('div');
        oDiv.appendChild(idiv);

    }
    if (main.childNodes.length == 0) { //判断添加时位置
        main.appendChild(oDiv);
    } else {
        main.insertBefore(oDiv, main.childNodes[0]);
    }
    var clickDiv = main.childNodes[0].childNodes[index]; //添加颜色
    clickDiv.setAttribute('class', 'i');
    clickDiv.style.backgroundColor = colors[index];
    // }

    //     }
}

//点击  判断
function binEvent() {
    main.addEventListener('click', function (e) { //绑定事件
        if (flag) { //点击锁
            var tar = e.target; //元对象
            if (tar.className == 'i') { //点击元素反应及条件
                tar.style.backgroundColor = '#fff';
                tar.classList.remove('i');
                num++;
            } else {
                alert('结束 ,得分:' + num); //结束加锁
                clearInterval(timer);
                flag = false;
            }
            if (num % 10 == 0) {
                speed++; //累加
            }
        }

    })
}
发布了50 篇原创文章 · 获赞 3 · 访问量 1120

猜你喜欢

转载自blog.csdn.net/weixin_45681557/article/details/104459348
今日推荐