完美运动框架,一定要理解下来,记下来呢

//获取非行内样式
function getStyle(obj,attr){
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,1)[attr];

}
//运动框架(完美运动框架)
function sport_07(obj,json,fn){
//1.清除上一次的计时器
clearInterval(obj.timer);
//2. 开启新的计时器
obj.timer = setInterval(function(){
//1. 假设一个开关
let stop = true; //假设所有的属性值都达到目标时为true
//2. 遍历对象
for(let attr in json){
//1. 获取当前值
let cur = attr === ‘opacity’ ? parseInt(parseFloat(getStyle(obj,attr)) * 100) : parseInt(getStyle(obj,attr));
//2. 计算速度
let speed = (json[attr] - cur) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
//3. 判断是否全部结束
if(cur !== json[attr]){
stop = false;
}
//4. 设置运动
if(attr === ‘opacity’){
obj.style.opacity = (cur + speed) / 100;
obj.style.filter = ‘alpha(opacity’ + (cur + speed) +’)’;
}else{
obj.style[attr] = cur + speed + ‘px’;
}

		console.log(cur,speed,json[attr]);
	}
	//3.停止计时器
	if(stop){
		clearInterval(obj.timer);
		if(typeof fn === 'function'){
			fn();
		}
	}
},30)

}

猜你喜欢

转载自blog.csdn.net/weixin_45052104/article/details/91284534