用js写一个九宫格内三个格子颜色随机闪动的效果。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>颜色随机的方块</title>
    <style>
        html{
            font-size: 10px;
        }
        *{
            padding: 0;
            margin: 0;
        }
        body{
            width: 100%;
        }
        .content{
            width: 40rem;
            height: 40rem;
            margin: 20px auto;
            background-color: #e2e2e2;
            border-radius: 5px;
        }
        .box{
            height: 100%;
            width: 100%;
            display: flex;
            flex-flow: row wrap;
            list-style: none;
            justify-content: space-around;
            padding: 0.5rem;
            box-sizing: border-box;
        }
        .box>li{
            width: 33.3%;
            height: 33.3%;
            padding: 0.5rem;
            box-sizing: border-box;
            border-radius: 3px;
            text-align: center;
            line-height: 15rem;
        }
        .box>li>div{
            width: 100%;
            height: 100%;
            background-color: yellow;
        }
        .btn{
            width: 40rem;
            margin: 0 auto; 
        }
        .btn>div{
            height: 2rem;
            text-align: center;
            width: 20%;
            margin: 0.5rem auto;
            line-height: 2rem;
            background-color: #e6e6e6;
            border-radius: 0.5rem;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="content">
      <ul class="box">
        <li><div class="let_style">1</div></li>
        <li><div class="let_style">2</div></li>
        <li><div class="let_style">3</div></li>
        <li><div class="let_style">4</div></li>
        <li><div class="let_style">5</div></li>
        <li><div class="let_style">6</div></li>
        <li><div class="let_style">7</div></li>
        <li><div class="let_style">8</div></li>
        <li><div class="let_style">9</div></li>
      </ul>
      <div class="btn">
        <div id="run">点击开始</div>
        <div id="stop">点击结束</div>
      </div>
    </div>
    <script>
        var list = document.getElementsByClassName('let_style');
        var run = document.getElementById('run');
        var stop = document.getElementById('stop');
        var time;
        function begin(one,two,three) {
             if(one == two || two == three || one == three){
                if(one == two){
                    one = Math.floor(Math.random()*list.length);
                }else if(two == three){
                    two = Math.floor(Math.random()*list.length);
                }else if(one == three){
                    three = Math.floor(Math.random()*list.length);
                }
                begin(one,two,three);
            }else{
                list[one].style.backgroundColor = 'rgb'+ colors();
                list[two].style.backgroundColor = 'rgb'+ colors();
                list[three].style.backgroundColor = 'rgb'+ colors();
            }
            //console.log()
           
        }
       
        function colors() {
            var rgb;
            var r = Math.floor(Math.random()*256);
            var g = Math.floor(Math.random()*256);
            var b = Math.floor(Math.random()*256);
            rgb = '('+r+','+g+','+b+')';
            return rgb;
        }
        run.onclick = function(){
            clearInterval(time);
            time = setInterval(function(){
                for(var i = 0; i < list.length; i++){
                   list[i].style.backgroundColor = '';
                }
                var one = Math.floor(Math.random()*list.length);
                var two = Math.floor(Math.random()*list.length);
                var three = Math.floor(Math.random()*list.length);
                begin(one,two,three);
 
            },1000);
        }
        stop.onclick = function(){
            clearInterval(time);
            for(var i = 0; i < list.length; i++){
                   list[i].style.backgroundColor = '';
            }
        }
 
 
    </script>
</body>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的九宫格抽奖程序的JavaScript代码: HTML代码: ```html <div id="lottery"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> <div class="item">7</div> <div class="item">8</div> <div class="item">9</div> </div> <button id="start">开始抽奖</button> ``` CSS代码: ```css #lottery { display: flex; flex-wrap: wrap; width: 300px; height: 300px; border: 1px solid #ccc; } .item { display: flex; align-items: center; justify-content: center; width: 100px; height: 100px; font-size: 24px; background-color: #fff; border: 1px solid #ccc; } ``` JavaScript代码: ```javascript const items = document.querySelectorAll('.item'); const startButton = document.querySelector('#start'); let isStarted = false; function getRandomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function startLottery() { if (isStarted) return; isStarted = true; let count = 0; const interval = setInterval(() => { items.forEach(item => item.classList.remove('active')); const index = getRandomNumber(0, items.length - 1); items[index].classList.add('active'); count++; if (count >= 10) { clearInterval(interval); isStarted = false; alert(`恭喜您抽中了${items[index].textContent}`); } }, 200); } startButton.addEventListener('click', startLottery); ``` 代码分析: 1. 获取九宫格的每个格子和开始抽奖的按钮。 2. 定义一个函数 `getRandomNumber` 用于获取指定范围内的随机整数。 3. 定义一个函数 `startLottery` 用于开始抽奖,其中使用 `setInterval` 定时器来随机选中一个格子并添加 `active` 类名,并且当随机次数达到10次时,清除定时器,并弹出抽中的格子的数字。 4. 监听开始抽奖按钮的点击事件,调用 `startLottery` 函数开始抽奖。 以上就是一个简单的九宫格抽奖程序的JavaScript代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值