动画速度由快到慢
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {
width: 100px;
height: 100px;
background-color: pink;
position: absolute;
left: 0;
opacity: 0.3;
}
</style>
</head>
<body>
<button id="btn200">200</button>
<button id="btn400">400</button>
<div id="box"></div>
</body>
</html>
<script>
var btn200 = document.getElementById("btn200");
var btn400 = document.getElementById("btn400");
var box=document.getElementById("box");
btn200.οnclick=function(){
animate(box,200);
}
btn400.οnclick=function(){
animate(box,400);
}
var timer=null;
function animate(obj,target){
obj.timer=setInterval(function(){
var speed=(target-obj.offsetLeft)/10;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
console.log(speed);
obj.style.left=obj.offsetLeft+speed+"px";
if(obj.offsetLeft==target){
clearInterval(obj.timer);
}
},50);
}
</script>