匀速运动 (当需要物体做匀速运动直接调用statmove函数)
1 function startmove(dom,targetposetion){ //dom : 运动对象,targetposition : 到达目标位置 2 clearinterval(timer); // 防止定时器叠加,先清除定时器。 3 var speed = targetposetion - dom.offsetleft > 0 ? 7 : -7; 4 //断物体到移动的目标位置的左边还是右边,左边速度是正的,右边速度是负的。 5 timer = setinterval(function(){ //设置定时器。变量timer要定义在函数外面。 6 if(math.abs(targetposetion - dom.offsetleft ) < math.abs(speed)){ 7 clearinterval(timer); 8 //当目位置减去物体偏移量小于定时器移动的位置(因为在移动就超出了目标位置),清除定时器 9 dom.style.left = targetposetion + "px"; 10 //因为距离目标位置还有一小段距离,直接让物体等于目标位置就行 11 }else{ 12 dom.style.left = dom.offsetleft + speed + "px"; 13 } 14 },30); 15 }
1 function startmove(dom, targetposetion) { 2 clearinterval(timer); 3 var speed = null; 4 timer = setinterval(function () { //设置定时器。变量timer要定义在函数外面。 5 speed = (targetposetion - dom.offsetleft) /10; 6 speed = speed > 0 ? math.ceil(speed) : math.floor(speed); 7 if(dom.offsetleft == targetposetion){ 8 clearinterval(timer); 9 }else{ 10 dom.style.left = dom.offsetleft + speed + "px"; 11 } 12 }, 30) 13 }
1 function getstyle(dom,attr){ 2 if(window.getcomputedstyle){ 3 return window.getcomputedstyle(dom,null)[attr]; 4 } 5 else{ 6 return dom.currentstyle[attr]; //ie8不兼容,ie独有属性currentstyle 7 } 8 }
1 startmove(div,25); 2 function startmove(dom, targetposetion) { 3 clearinterval(timer); 4 var speed = null,current = null; 5 timer = setinterval(function () { 6 current = parsefloat(getstyle(div,'opacity')) * 100; 7 console.log(current); 8 speed = (targetposetion - current) / 10; 9 speed = speed > 0 ? math.ceil(speed) : math.floor(speed); 10 if (current == targetposetion) { 11 clearinterval(timer); 12 } else { 13 dom.style.opacity = (current + speed) / 100; 14 } 15 }, 30) 16 }
1 function startmove(dom , attr , target){ 2 clearinterval(dom.timer); 3 dom.timer = setinterval(function(){ 4 var current = null,speed = null; 5 if(attr == 'opacity'){ 6 current = parsefloat(getstyle(dom,attr)) * 100; 7 }else{ 8 current = parsefloat(getstyle(dom,attr)); 9 } 10 speed = (target - current) / 10; 11 speed = speed > 0 ? math.ceil(speed) : math.floor(speed); 12 if(current == target){ 13 clearinterval(dom.timer); 14 } 15 if(attr == 'opacity'){ 16 dom.style.opacity = (current + speed) / 100; 17 }else{ 18 dom.style[attr] = (current + speed) + "px"; 19 } 20 },30) 21 } 22 23 var div = document.getelementsbytagname('div'); 24 div[0].onmouseenter = function(){ 25 startmove(this,'width',400); 26 }; 27 div[1].onmouseenter = function(){ 28 startmove(this,'height',400); 29 }; 30 div[2].onmouseenter = function(){ 31 startmove(this,'borderwidth',20); 32 }; 33 div[3].onmouseenter = function(){ 34 startmove(this,'opacity',50); 35 };
多物体多值运动 + 回调机制
function startmove(dom, targetobj, callback) { clearinterval(dom.timer); dom.timer = setinterval(function () { var stop = true; var speed = null, current = null; for (var prop in targetobj) { if (prop == 'opacity') { current = parsefloat(getstyle(dom, prop))*100; } else { current = parsefloat(getstyle(dom, prop)); } speed = (targetobj[prop] - current) / 10; speed = speed>0?math.ceil(speed):math.floor(speed); if (prop == 'opacity') { dom.style.opacity = (current + speed) / 100; } else { dom.style[prop] = (current + speed) + "px"; } if (targetobj[prop] != current) { stop = false; } } if (stop == true) { clearinterval(dom.timer); typeof callback == 'function' && callback(); } }, 30); }
//让多物体多值运动,由于多值就可以用对象来装。
startmove(dom, targetobj, callback)接收3个参数,运动对象,想要改变的多值属性对象,回调函数
跟上面一样一地步清除定时器,然后设置定时器,用for in 循环传过来的对象属性,
如果传过来的是opacity 就要扩大100倍,不是就正常调用上面getstyle()方法,返回一个计算属性。
设置一个锁,每次定时器执行时定义一个变量stop = true 可以停止,
当for in 循环时,判断如果有一个当前值不等于目标值的时候,就让stop = false。不可以停止
for in 结束,stop == true 的时候就代表所有的值都到达目标值,这时候就可以清空定时器。这个运动过程结束,调用回调函数
1 var divone = document.getelementbyid('one'); 2 var divtwo = document.getelementbyid('two'); 3 divone.onclick = function () { 4 startmove(this, { width: 400,height: 400,left: 200,top: 300,opacity: 50}, function () { 5 startmove(divtwo, { width: 400, height: 400, left: 200, top: 300, opacity: 50 }, function () { 6 alert(555); 7 }) 8 }) 9 }
如对本文有疑问, 点击进行留言回复!!
2020-07-21 html5如何监听veido的全屏 + css的字体间距 + JS的window.open() + 软技能svn与git
html+css+js适合前端小白的实战全解(超详细)——2048小游戏(二)
在使用@angular/cli创建的angular项目上添加postcss等一系列移动端自适应插件
网友评论