<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> body,div{ margin: 0px; padding: 0px; } div{ width:200px; height: 200px; background: red; margin:10px; float: left; opacity: 0.3; } </style> <script type="text/javascript"> window.onload=function(){ var oDiv= document.getElementsByTagName('div'); for (var i=0;i<oDiv.length;i++){ oDiv[i].alpha=30;//若多个物体要进行自己的运动,不造成其他物体的影响,需对每个物体定义一个alpha.也可设置oDiv[i].timer = null; oDiv[i].onmouseover=function(){ startMove(this,100);//this为当前的物体 } oDiv[i].onmouseout=function(){ startMove(this,30); } } } //obj被套用为this,从而可以在每个alpha或timer上,进行套用. function startMove(obj,iTarget){ clearInterval(obj.timer);//先要将原本的动画效果取消. obj.timer = setInterval(function(){ var speed = 0; if(obj.alpha>iTarget){ speed = -10; } else{ speed = 10; } if(obj.alpha == iTarget){ clearInterval(obj.timer); } else{ obj.alpha+=speed; obj.style.opacity = obj.alpha/100; } },30) } </script> </head> <body> <div></div> <div></div> <div></div> <div></div> </body> </html>
obj.alpha不是==iTarget时就清除动画了吗?
qq_杀意隆_0