这里需要注意:
1. 在开始运动时关闭已有的定时器
2. 把运动和停止隔开
复制代码 代码如下:
<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="utf-8">  
        <title></title>  
        <style type="text/css">  
            #div1{  
                width: 200px;  
                height: 200px;  
                background: red;  
                position: absolute;  
                left:0;  
                top:60px;  
            }  
        </style>  
        <script type="text/javascript">  
            window.onload=function(){  
                var oDiv=document.getElementById("div1");  
                var oBt=document.getElementsByTagName('input')[0];  
                var time=null;  
                oBt.onclick=function(){  
                    clearInterval(time);//这里首先要关闭一个定时器,这是因为解决在运动过程中多次点击按钮从而产生多个定时器叠加的bug  
                    time=setInterval(function(){  
                        var speed=7;  
                        if(oDiv.offsetLeft<=600)  
                        oDiv.style.left=oDiv.offsetLeft+speed+'px';  
                        else{  
                            clearInterval(time);  
                        }  
                    },30);  
                }  
            }  
        </script>  
    </head>  
    <body>  
        <input type="button" value="开始运动" />  
        <div></div>  
    </body>  
</html>
