匀速运动案例

编辑本博客

  • 通过setInterval做动画
  • 动画运动到一定位置需要用clearInterval(inter)将定时器进行清理

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>匀速运动案例</title>
    <style type="text/css">
        #box{
            position: relative;/*相对定位指相对自己原先的位置*/
            width: 50px;
            height: 50px;
            top: 200px;
            background-color: #A9A9A9;
            }
    </style>
</head>
<body>
    <button id="btn">运动</button>
    <div id="box">
    </div>
</body>
<script type="text/javascript">
    var btn=document.getElementById("btn");
    var box=document.getElementById('box');
    var count=0;
    var inter=null;
    btn.onclick=function (ev) {
        inter=setInterval(function () {
            count++;
            box.style.left=count+'px';
            if(count>1000){
                clearInterval(inter);
                box.style.display='none';
            }
        },10)
    }
</script>
</html>
View Code

 

posted @ 2018-06-16 19:15  丫丫625202  阅读(142)  评论(0编辑  收藏  举报