js--缓冲运动

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" charset="utf-8" />
    <meta http-equiv="content-type" content="text/html" />
    <title>demo</title>
</head>
<body>
<div id="odiv" style="position:absolute;200px;height:200px;background:red;left:0;"></div>
<div id="odiv1" style="position:absolute;200px;height:200px;background:red;left:0;top:250px;"></div>

<script type="text/javascript">
    var odiv=document.getElementById('odiv');
    var odiv1=document.getElementById('odiv1');
    odiv.onclick=function(){move(odiv,500);}
    odiv1.onclick=function(){move(odiv1,500);}
    function move(obj,target){
        clearInterval(dt);
        var dt=setInterval(function(){
            var ol=parseInt(obj.style.left);
            if(ol==target){
                clearInterval(dt);
            }else{
                var speed=(target-ol)/8;
                speed>0?speed=Math.ceil(speed):speed=Math.floor(speed);
                obj.style.left=ol+speed+"px";
            }
        },30);
    }

</script>
</body>
</html>
View Code
原文地址:https://www.cnblogs.com/lanyueff/p/5006631.html