html+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>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    .main {
        width: 400px;
        height: 650px;
        margin: 10px auto;
        border: 1px solid red;
        background: #F06060;
    }

    .start {
        width: 300px;
        height: 100px;
        line-height: 100px;
        text-align: center;
        margin: 250px auto;
        font-size: 30px;
        font-weight: 600;
        color: #fff;
        border: 5px solid red;
        border-radius: 20px;
        display: none;
        cursor: pointer;
    }

    ul {
        display: flex;
        flex-wrap: wrap;
        width: 380px;
        height: 380px;
        margin: 50px 10px;
        list-style: none;

    }

    li {
        border: 1px solid #fff;
    }

    .li1 {
        width: 50%;
        height: 50%;
    }

    .li2 {
        width: 33.33%;
    }

    .li3 {
        width: 25%;
    }

    .li4 {
        width: 20%;
    }

    .li5 {
        width: 16.66%;
    }

    .li6 {
        width: 14.28%;
    }

    .li7 {
        width: 12.5%;
    }

    .li8 {
        width: 11.11%;
    }

    .li9 {
        width: 10%;
    }

    .li10 {
        width: 9.09%;
    }

    .li11 {
        width: 8.33%;
    }

    .li12 {
        width: 7.69%;
    }

    .li13 {
        width: 7.14%;
    }

    .li14 {
        width: 6.66%;
    }

    .li15 {
        width: 6.25%;
    }

    .li16 {
        width: 5.88%;
    }

    .li17 {
        width: 5.55%;
    }

    .li18 {
        width: 5.26%;
    }

    .li19 {
        width: 5%;
    }

    .li20 {
        width: 4.76%;
    }

    .score {
        position: fixed;
        bottom: 200px;
        left: 50%;
        transform: translateX(-50%);
        font-size: 30px;
        color: #fff;
    }

    .countDown {
        position: fixed;
        top: 15px;
        left: 50%;
        transform: translateX(-50%);
        font-size: 30px;
        color: #fff;
    }
</style>

<body>
    <div class="main">
        <!-- 重新开始 -->
        <div class="countDown"></div>
        <p class="start"></p>
        <p class="score"></p>
        <ul>
        </ul>
    </div>
</body>
<script>
    var ul = document.querySelector('ul');
    var start = document.querySelector('.start');
    var score = document.querySelector('.score');
    var i = 2;
    var b = 1;
    var scoreValue = 0;
    addRect();
    function addRect() {
        // 随机颜色
        var color = Math.floor(Math.random() * 16777215).toString(16);
        ul.innerHTML = '';
        for (var a = 0; a < i * i; a++) {
            var li = document.createElement('li');
            ul.appendChild(li);
            li.className = 'li' + b;
            li.style.backgroundColor = '#' + color;
        }
        var index = Math.floor(Math.random() * i * i);
        ul.children[index].style.opacity = 0.8;
        ul.children[index].addEventListener('click', function () {
            scoreValue++;
            score.innerHTML = '得分:' + scoreValue;
            if (b < 20) {
                b++;
                i++;
                addRect();
            } else {
                ul.style.visibility = 'hidden';
                start.innerHTML = '牛!点击重开';
                start.style.display = 'block';
                countDown.style.display = 'none';
            }
        })
    }
    // 点击重开
    start.addEventListener('click', function () {
        ul.innerHTML = '';
        ul.style.visibility = 'visible';
        start.style.display = 'none';
        countDown.style.display = 'block';
        i = 2;
        b = 1;
        scoreValue = 0;
        score.innerHTML = '';
        addRect();
        time = 120;
        getCountDown();
    })
    // 两分钟倒计时
    var time = 120;
    var countDown = document.querySelector('.countDown');
    getCountDown();
    function getCountDown() {
        // 倒计时结束
        if (time == 0) {
            start.innerHTML = '时间到!点击重开';
            ul.style.visibility = 'hidden';
            start.style.display = 'block';
            countDown.style.display = 'none';
        }
        countDown.innerHTML = time;
        time--;
    }
    var clock = setInterval(getCountDown, 1000)
</script>

</html>

效果

请添加图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z_yuyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值