function my$(id) { return document.getElementById(id); } function animate(element,target) { clearInterval(element.timer); element.timer = setInterval(function () { var step = 10; var current = element.offsetLeft; step=target>current?step:-step; current+=step; if (Math.abs(target-current)>Math.abs(step)){ element.style.left=current+"px"; }else { clearInterval(element.timer); element.style.left=target+"px"; } //测试代码 console.log("目标位置:"+target+",当前位置:"+current+",每次移动步数:"+step); },20); }
匀速动画函数封装代码
最新推荐文章于 2022-05-08 00:00:57 发布