首页 > 其他 > 详细

2016-05-30 定时器

时间:2016-05-31 00:52:11      阅读:141      评论:0      收藏:0      [点我收藏+]

1.

<body>
<input id="btn" type="button" value="按钮">
<div id="div"></div>
<script>
    var btn= document.getElementById(‘btn‘);
    var odiv=document.getElementById("div");
    //var timer=null;
    odiv.timer=null;

    btn.onclick=function(){
        clearInterval(odiv.timer);//防止动画在执行过程中,人为的在不停的点击按钮,不断产生定时器
        odiv.timer=setInterval(function(){
            var speed =getStyle(odiv,‘left‘,50);//odiv 往右移动50px
            if(speed>900){
                speed=900;
            }
            odiv.style.left=speed+‘px‘;//每隔0.1s往右移动10px
            if(speed==900){//停止移动
                clearInterval(odiv.timer);
                alert(speed);
            }
        },200);
    }

    function getStyle(obj,attr,step){//为什么要parseInt(),因为obj.currentStyle[attr] 拿到的是30px -->30
        return parseInt(obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[attr])+step ;
    }

</script>
</body>

 

2016-05-30 定时器

原文:http://www.cnblogs.com/bravolove/p/5544473.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!