发表于: 2020-05-29 14:24:29

1 2270



<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <style>
*{
    margin0;
    padding0;
}
.border{
    width375px;
    height489px;
    border1px solid #1b6ca8;
    border-radius10px;
    margin10% auto;
}
table{
    margin0 auto;
}
td{
    width120px;
    height120px;
    background-color:#0a97b0;
    border-radius10px;
}
.ctrl{
    margin0 auto;
    width368px;
    height120px;
}
input{
    width368px;
    height50px;
    background-color:white;
    border2px solid #ffd3e1;
    margin5px auto ;
    border-radius10px;
    font-size18px;
    color#0a97b0;
    cursorpointer;
    outlinenone;
}
input:hover{
    background-color:#ffd3e1;
}

        </style>
    </head>
    <body>
        <div class="border">
            <table>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </table>
            <div class="ctrl">
                <input type="button" value="开始" class="start">
                <input type="button" value="停止" class="end">
            </div>
        </div>
    </body>

    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            var time;
            //生成三个随机的td
            function begin(one,two,three) {
             if(one == two || two == three || one == three){
                if(one == two){
                    one = Math.floor(Math.random()*$('td').length);
                }else if(two == three){
                    two = Math.floor(Math.random()*$('td').length);
                }else if(one == three){
                    three = Math.floor(Math.random()*$('td').length);
                }
                begin(one,two,three);
            }else{
                    $('td')[one].style.backgroundColor = 'rgb'colors();
                    $('td')[two].style.backgroundColor = 'rgb'colors();
                    $('td')[three].style.backgroundColor = 'rgb'colors();
            }    
        }
            //随机生成rgb值
            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;
            };

            $('.start').click(function(){
                //清空上一次执行的time();
                clearInterval(time);

                time = setInterval(function time (){
                    for(var i = 0;i < $('td').lengthi++){
                        $('td')[i].style.backgroundColor = '';
                        }
                var one = Math.floor(Math.random()*$('td').length);
                var two = Math.floor(Math.random()*$('td').length);
                var three = Math.floor(Math.random()*$('td').length);
                begin(one,two,three);
                },1000);
                
            })

            $('.end').click(function(){

                

                clearInterval(time);

                $('td').each(function(){
                    $('td').css("backgroundColor","#0a97b0");
                    })
            })
        })
    </script>
</html>

任务一完成的事情:

1.遇到input无法居中的问题,只要在input外套一个div即可;

2.学习了如何使用随机数;

3.避免重复执行,可以先清除上一次执行的方法


返回列表 返回列表
评论

    分享到