封装获取行内样式
function getStyle(obj,attr){
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,1)[attr];
}
实现完美运动框架
function sport(obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
let flag = true;
for(let attr in json){
let current = attr === 'opacity' ? Math.floor(parseFloat(getStyle(obj,attr)) * 100) : parseInt(getStyle(obj,attr);
let speed = (json[attr] - current ) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if(json[attr] !== current){
flag = false;
}
if(attr === 'opacity'){
obj.style.opacity = (current + speed) / 100;
obj.style.filter = 'alpha(opacity =' + (current + speed) + ')';
}else{
obj.style[attr] = current + speed + 'px';
}
}
if(flag){
clearInterval(obj.timer);
if(fn instanceof Function){
fn();
}
}
},30)
}