匀速运动封装

function indeIm(btnId,boxId,step,target){
		var btn=document.getElementById(btnId);
		var box=document.getElementById(boxId);
		//定义变量
		var timer=null,begin=0;
		//监听点击事件
		btn.onclick=function(){
			//清除定时器
			clearInterval(timer);
			//启动定时器
			timer=setInterval(function(){
				//相加
				begin+=step;
				//判断
				if(begin>=target){
					begin=target;
					clearInterval(timer);
				}
				console.log(begin);
				//动
				box.style.marginLeft=begin+'px';		
			},100)
		}
	}

猜你喜欢

转载自blog.csdn.net/weixin_42500793/article/details/81412734
今日推荐