码迷,mamicode.com
首页 > Web开发 > 详细

js 运动01

时间:2016-10-07 07:36:58      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

1. 运动中的四个bug

不会停止; 速度取某些值会无法停止; 到达位置后再点击还会运动; 重复点击速度加快

<input  type="button" onclick="startMove()" value="开始运动"/>
<div id="div1"></div>
<script>
    var timer = null;
    function startMove(){
        var oDiv = document.getElementById("div1");
        var iSpeed = 10;
        clearInterval(timer);//确保多次点击只开一个定时器
        timer = setInterval(function(){
            if(oDiv.offsetLeft >= 300){
                clearInterval(timer)
            }else{
                oDiv.style.left = oDiv.offsetLeft + iSpeed + ‘px‘;
            }
        },30);
    }
</script>

  

js 运动01

标签:

原文地址:http://www.cnblogs.com/bravolove/p/5935322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!