我用DOM2级绑定多个事件,为什么要把定时器取消才能执行?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>链式动画</title> <style type="text/css"> *{margin:0;padding:0;} ul,li{ list-style:none; } ul li{ width:200px; height:100px; background:yellow; margin-bottom:20px; border:4px solid #000; filter:alpha(opacity=30); opacity:0.3; } </style> <script src="move.js"></script> </head> <body> <ul> <li id="li1"></li> </ul> <script type="text/javascript"> var Li=document.getElementById('li1'); Li.addEventListener('mouseover',function(){startMove(Li,'width',400);},false); Li.addEventListener('mouseover',function(){startMove(Li,'height',200);},false); Li.addEventListener('mouseover',function(){startMove(Li,'opacity',100);},false); </script> </body> </html>
接下来是引入的js部分
function getStyle(obj,attr){//传入两个参数:对象和属性 if (obj.currentStyle) { //currentStyle针对ie浏览器 return obj.currentStyle[attr]; } else { //getComputerStyle针对firefox浏览器 return getComputedStyle(obj,false)[attr]; } } // var timer=null; function startMove(obj,attr,iTarget,fn){ // clearInterval(obj.timer); obj.timer=setInterval(function(){ //1、取当前的值 var icur=0; if (attr=='opacity') { icur=Math.round(parseFloat(getStyle(obj,attr))*100); //*100是为了和17行代码中的透明度对应 } else { icur=parseInt(getStyle(obj,attr)); } //2、计算速度 var speed=(iTarget-icur)/8; speed=speed>0?Math.ceil(speed):Math.floor(speed); //3、检测停止 if (icur==iTarget) { clearInterval(obj.timer); if (fn) { fn(); } } else { if (attr=='opacity') { obj.style.filter='alpha(opacity='+(icur+speed)+')';//针对ie浏览器 obj.style.opacity=(icur+speed)/100;//针对firefox和chrome浏览器的 } else { // obj.style.width=icur+speed+'px'; obj.style[attr]=icur+speed+'px'; } } },30) }