原生javascript运动函数的封装

原生javascript运动函数的封装的几种方式

 


//封装匀速运动

//参数:
// 1、dom对象
// 2、样式属性(top,left,width,height,opacity等等)
// 3、起始位置,结束位置
// 4、速度:时间间隔,步长
// 5、方向:
//返回值

function moveObj(domObj,attr,startValue,endValue,timeSpace,step,direction) {
	
	let currValue = startValue;

	let myTimer = setInterval(function(){
		//1、改变数据
		currValue = currValue+direction*step;

		//2、判断边界
		// if(currValue>=endValue){//??
		// 	currValue = endValue;//??
		// 	window.clearInterval(myTimer);
		// }

		if(Math.abs(currValue-endValue)0?1:-1;//??	
	let timeSpace = 10;
	let step = Math.abs(endValue-startValue)/(timeLong/timeSpace) // endValue-startValue/步子数;//
	
	let currValue = startValue;
	let myTimer = setInterval(function(){
		//1、改变数据
		currValue = currValue+direction*step;

		//2、判断边界
		if(Math.abs(currValue-endValue)=offsetX){
			left1 = offsetX;
			top1=Math.sqrt(2*p*left1);
			window.clearInterval(myTimer);
			if(func){
				func();
			}
		}

		//3、改变外观			
		domObj.style.left = left1+startPoint.x+"px";
		domObj.style.top = top1+startPoint.y+"px";
		
	},timeSpace);
}

//淡入:
//参数:
// dom对象
// 时长;
//返回值:无

function fadeIn(domObj,timeLong){
	domObj.style.opacity = 0;
	moveObj02(domObj,"opacity",1,timeLong);
}


//淡出:
//参数:
// dom对象
// 时长;
//返回值:无

function fadeOut(domObj,timeLong){
	domObj.style.opacity = 1;
	moveObj02(domObj,"opacity",0,timeLong);
}


//淡入和淡出:
//参数:
// domObjIn:淡入的dom对象
// domObjOut:淡出的dom对象
// 时长;
//返回值:无

function fadeInOut(domObjIn,domObjOut,timeLong,func){
	domObjIn.style.opacity = 0;
	domObjOut.style.opacity = 1;

	let startValue = 0;
	let endValue = 1;
	let direction= 1;
	let timeSpace = 10;
	let step = Math.abs(endValue-startValue)/(timeLong/timeSpace) // endValue-startValue/步子数;//
	
	let currValue = startValue;
	let myTimer = setInterval(function(){
		//1、改变数据
		currValue = currValue+direction*step;

		//2、判断边界
		if(Math.abs(currValue-endValue)0?1:-1;//??	
	let directionObj = {};
	for(let key in endObj){
		directionObj[key] = endObj[key]>startObj[key]?1:-1;
	}

	let timeSpace = 10;
	// let step = Math.abs(endValue-startValue)/(timeLong/timeSpace) // endValue-startValue/步子数;//
	let stepObj = {};	
	for(let key in endObj){
		stepObj[key] = Math.abs(endObj[key]-startObj[key] )/(timeLong/timeSpace);
	}

	//let currValue = startValue;
	let currObj = {};
	for(let key in endObj){
		currObj[key] = startObj[key];
	}

	let myTimer = setInterval(function(){
		//1、改变数据
		//currValue = currValue+direction*step;
		for(let key in endObj){
			currObj[key] = currObj[key]+directionObj[key]*stepObj[key];
		}
		//2、判断边界
		let firstKey = Object.keys(endObj)[0];
		if(Math.abs(currObj[firstKey]-endObj[firstKey])

 

你可能感兴趣的:(原生javascript)