-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Var btn1=document. GetElementById (" btn1 ");
Var btn2=document. GetElementById (" btn2 ");
Var box1=document. GetElementById (" box1 ");
Var box2=document. GetElementById (' box2 ');
Btn1. Onclick=function () {
The animate (box1, 200);
}
Btn2. Onclick=function () {
The animate (box2, 300);
}
The function the animate (element, traget) {
If (element. TimeId is) {
ClearInterval (element. TimeId is);
TimdId=null;
}
Element. TimeId is=setInterval (fn, 30);
Var current=element. The offsetLeft;
The function fn () {
var speed=10;
The current=current & gt; Traget? Current: the current +=speed;
Element. Style. Left=current + 'px';
}
}
CodePudding user response:
Use requestAnimationFrame is more efficient, you try
CodePudding user response: