案例18-移动的盒子


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <style> 

        div{

            width:240px;

            height: 240px;

            background: url('img/run.jpg');

            position: absolute;

            left: 150px;

            top: 0;

        }

        button{

            position: absolute;

            top: 300px;

            left: 75px;

        }

        button:nth-of-type(1){

            left: 75px;

        }

        button:nth-of-type(2){

            left: 175px;

        }

        span{

            position: absolute;

            width: 600px;

            height: 20px;

            text-align: right;

            color: #fff;

            background: red;

            left: 75px;

            top: 218px;

            padding-right: 10px;

            font-weight: bold;

        }

    </style>

</head>

<body>

    <div></div>

    <button>跑</button>

    <button>停</button>

    <span>终点</span>

    <script>

        var btn = document.querySelectorAll('button');

        var div = document.querySelector('div');

        var num = 0;

        var timer = null;

        function getStyle(obj){

            if(obj.currentStyle){

                return obj.currentStyle;

            }else{

                return getComputedStyle(obj);

            } 

        }

        /*决定是否开启第二次或者说更多次的定时器的变量*/

        // var onOff = true;

        /*第一种方法:每次开启定时器之间,都清空上一次开启的定时器*/

        btn[0].onclick = function(){

            div.style.background = 'url(img/download.gif)';

            /*获取当前元素所在的位置*/

            var num = parseInt(getStyle(div).left);

            clearInterval(timer);

            timer = setInterval(function(){

                num++;

                div.style.left = num + 'px';

            },16);

        }

        btn[1].onclick = function(){

            clearInterval(timer);

            div.style.background = 'url(img/run.jpg)';

        }

        /*第二种方法:利用开关*/

        // btn[0].onclick = function () {

        //         div.style.background = 'url(img/download.gif)';

 

        //         // if(true){

        //         //     timer = setInterval(function () {

        //         //         num++;

        //         //         div.style.left = num + 'px';

        //         //     }, 16);

        //         // }

        //         if(!onOff){

        //             return;

        //         }

        //         onOff = false;

        //         timer = setInterval(function () {

        //             num++;

        //             div.style.left = num + 'px';

        //         }, 16);

 

        //     }

        // btn[1].onclick = function(){

        //     clearInterval(timer);

        //     onOff = true;

        //     div.style.background = 'url(img/run.jpg)';

        // }

        /*

            1- 每次开启定时器前,清除上一次的定时器

            2- 开关

        */

    </script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值