完美运动框架

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36369717/article/details/53025286
//获取样式
function getStyle(obj,attr){
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,false)[attr];
}

//完美运动
function startMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var stop = true;
for(var attr in json){
var cur = 0;
//设置初值
if(attr == 'opacity'){
cur = parseInt(parseFloat(getStyle(obj,attr)) * 100);
}else{
cur = parseInt(getStyle(obj,attr));
}

//计算速度
var speed = (json[attr] - cur) / 10;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

//判断停止
if(cur != json[attr]){
stop = false;
}

if(attr == 'opacity'){
obj.style.filter ='alpha(opacity=' + (cur + speed) + ')';
obj.style.opacity = (cur + speed) / 100;
}else{
obj.style[attr] = (cur + speed) + 'px';
}
}
if(stop){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30);
}
展开阅读全文

没有更多推荐了,返回首页