// 动画效果
function startMove(obj,json,fn){
clearInterval(obj.timer);
var flag = true;
obj.timer = setInterval(function(){
for(var i in json){
// 1.取当前值
var oCur = 0;
if(i == 'opacity'){
oCur = Math.round(parseFloat(getStyle(obj,i))*100);
}else{
oCur = parseInt(getStyle(obj,i));
}
// 2.计算速度
var speed = (json[i]-oCur)/20;
speed = speed > 0 ? Math.ceil(speed):Math.floor(speed);
console.log(speed)
// 3.检测停止
if(oCur != json[i]){
flag = false;
}else{
flag = true;
}
if(i == 'opacity'){
curNum = speed + oCur;
obj.style.filter = 'alpha(opacity:'+curNum+')';
obj.style.opacity = curNum/100;
}else{
obj.style[i] = oCur + speed +'px';
}
}
if(flag){
clearInterval(obj.timer);
fn && fn();
}
},10)
}
// 获得css属性值
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
原生js简单实现jQuery的动画效果
最新推荐文章于 2022-11-14 15:54:11 发布