JS函数动画(匀速上下左右运动)封装

function uniformMove(obj,attr,targetPosition,speed,time){
				clearInterval(obj.timer)
				obj.timer = setInterval(function(){
					//通过属性名获得属性值
					//getStyle(obj,attr) 是字符串"~px"
					var attrvalue=parseInt(getStyle(obj,attr));
					//var attrvalue=parseInt(getComputedStyle(obj)[attr]);
					if(Math.abs(targetPosition-attrvalue)<=Math.abs(speed)){
						obj.style[attr]=targetPosition+"px";
						clearInterval(obj.timer);
					}else{
						obj.style[attr]=attrvalue+speed+"px";
					}
				console.log("offset: "+ attrvalue);
				},time);
			}

//获得CSS样式属性值:

function getStyle(obj,attr){
			if(obj.currentStyle){
					//支持IE
					return obj.currentStyle[attr];
				}else{
					return getComputedStyle(obj)[attr];
				}
			}

猜你喜欢

转载自blog.csdn.net/weixin_39200549/article/details/82959193
今日推荐