function easeMove(obj,attr,targetPos,speedEnd,time){
clearInterval(obj.timer)
obj.timer = setInterval(function(){
var attrValue = parseInt(getStyle(obj,attr));
var speed = Math.abs(targetPos - attrValue)/speedEnd
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(attrValue == targetPos){
clearInterval(obj.timer);
}else{
obj.style[attr] = attrValue + speed + "px";
}
console.log(getStyle(obj,attr));
},time)
}
//获得CSS样式属性值:
function getStyle(obj,attr){
if(obj.currentStyle){
//支持IE
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj)[attr];
}
}
本文介绍了一个使用JavaScript实现的平滑动画效果,通过调整速度和时间参数来控制元素的移动过程,实现了从当前位置到目标位置的平滑过渡。同时,文章还提供了一种获取元素CSS样式的通用方法。
375

被折叠的 条评论
为什么被折叠?



