首页 > Web开发 > 详细

缓冲运动-1-[解决1].html

时间:2014-10-21 17:42:22      阅读:175      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        body{margin:0;padding:0;}
        div{width:100px;height:100px;background:red;position: absolute;left:0;}
        span{width:0;height:600px;position:fixed;left:600px;top:0;border-left:1px solid red;}
    </style>
</head>
<body>
<input type="button" value="buttons" onclick="startMove(600)"/>
<div></div>
<span></span>
</body>
</html>
<script type="text/javascript">
    var timer=null; //定时器需放在函数外面,避免每次调用都开启一个
    function startMove(iTarget)
    {
    var oDiv=document.getElementsByTagName("div")[0];
    clearInterval(timer);//移出所有的定时器,解决了每次点击都开启一个定时器的问题
    timer=setInterval(function(){
        var Speed=Math.ceil((iTarget-oDiv.offsetLeft)/10);//解决


        if(oDiv.offsetLeft>=iTarget){clearInterval(timer);} //已经到达,匀速运动if里面的条件必须为>=
       else{oDiv.style.left=oDiv.offsetLeft+Speed+"px"; document.title=oDiv.style.left;}//未到达
    },30)


    }
</script>

缓冲运动-1-[解决1].html

原文:http://blog.csdn.net/xiaomogg/article/details/40347711

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