JS基础-定时器应用3(精)

取消了全局timer变量函数,使用obj.timer作为定时器标示符
增加了回调函数callback()

<!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>
        *{
            margin: 0%;
            padding: 0%;
        }
        #box1{
            width: 100px;
            height: 100px;
            background-color:red;
            /* 如果需要移动位置,就必须要开启位置定位 */
            position: absolute;
        }
        #box2{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
            top: 200px;
        }
    </style>
    <script>
        window.onload = function(){
            var btn1 = document.getElementById('btn1');
            var btn2 = document.getElementById('btn2');
            var btn3 = document.getElementById('btn3');
            var box1 = document.getElementById('box1');
            var box2 = document.getElementById('box2');

            
            // 向右移动
            btn1.onclick = function(){
                // clearInterval(timer);
                // // 开启一个定时器,用来执行动画效果.
                // timer = setInterval(function(){
                //     // 获取box1的原来left值,parseInt()函数可解析一个字符串,并返回一个整数。
                //     // 也可以用box1.style.left来获取left值,但使用getStyle()函数更加高级一些.
                //     var oldValue = parseInt(getStyle(box1, 'left'));
                //     var newValue = oldValue + 20;
                //     box1.style.left = newValue + 'px';
                //     if(newValue >= 800){
                //         clearInterval(timer);
                //         alert(box1.style.left);
                //     };
                // }, 50);
                move(box1, 'left', 800, 20);
            };
            // 向左移动
            btn2.onclick = function(){
                // clearInterval(timer);
                // timer = setInterval(function(){
                //     var oldValue = parseInt(getStyle(box1, 'left'));
                //     var newValue = oldValue - 20;
                //     box1.style.left = newValue + 'px';
                //     if (newValue <=0){
                //         clearInterval(timer);
                //     };
                // }, 50);
                move(box1, 'left',  0, 20);
            };

            btn3.onclick = function(){
                move(box2, 'left', 800, 20);
            };

            btn4.onclick = function () {
                move(box2, 'width', 800, 20, function(){
                    move(box2, 'height', 400, 20);
                    alert('这是一个callback执行函数');
                });
            };
        };

        // 定义一个变量,用来保存定时器标识
        // 所有正在执行的定时器都在这个变量中保存,当有多个元素要执行动画时,容易出错,所以这里不能用全局变量
        // var timer;

        // 写一条动画的函数:
        /*
        参数:
            obj:要执行动画的对象
            attr:要执行动画的样式,比如:left top width height
            target:执行动画的目标位置
            speed:移动的速度(正数向右移动,负数向左移动)
            callback:回调函数,这个函数将会在动画执行完毕以后执行
        */
        function move(obj, attr, target, speed, callback){
            // 关闭上一个定时器
            clearInterval(obj.timer);

            // 获取元素目前位置
            var current = parseInt(getStyle(obj, attr));

            // 判断速度正负值
            // 如果从0向800移动,则speed为正
            // 如果从800向0移动,则speed为负
            if(current > target){
                speed = -speed;
            };

            // 开启一个定时器,用来执行动画效果
            // 向执行动画的对象中添加一个timer属性,用来保存它自己的定时器的标识.
            obj.timer = setInterval(function(){
                var oldValue = parseInt(getStyle(obj, attr));
                // 在旧值的基础上添加
                var newValue = oldValue + speed;

                // 判断newValue是否大于800
                // 从800向0移动
                // 向左移动时,需要判断newValue是否小于target
                // 向右移动时,需要判断vewValue是否大于target
                if(speed < 0 && newValue < target || speed > 0 && newValue > target){
                    newValue = target;
                };
                // 将新值设置给box1
                // 因为是变量,所以这里不能使用style.attr格式写,必须使用[attr]
                obj.style[attr] = newValue + 'px';

                // 当元素移动到0px时,将其停止执行动画
                if(newValue == target){
                    // 达到目标,关闭定时器
                    clearInterval(obj.timer);

                    // 动画执行完毕,调用回调函数
                    // callback()
                    // 如果按照上面的写法,callback函数则为必须要传入的函数.
                    // 使用下面这种方法,则callback作为可选项函数
                    // &&与的关系,  ||或的关系
                    callback && callback();
                };
            }, 50);
        }

        /*
            定义一个函数, 用来获取指定元素的当前样式
            参数:
            obj 要获取样式的元素
            name 要获取的样式名
        */
        function getStyle(obj, name){
            if(window.getComputedStyle){
                // 正常游览器的方式,具有getComputedStyle()方法
                return getComputedStyle(obj, null)[name];
            }else{
                // IE8的方式,没有getComputedStyle()方法
                return obj.currentStyle[name];
            };
        };
    </script>
</head>
<body>
    <button id="btn1">点击box1向右移动</button>
    <button id="btn2">点击box1向左移动</button>
    <button id="btn3">点击box2向右移动</button>
    <button id="btn4">点击box2 width向右移动</button>
    <br><br>
    <div id="box1"></div>
    <div id="box2"></div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值