不知道錯(cuò)在哪了??
<!doctype?html> <html> <head> <meta?charset="utf-8"> <title>hello?world</title> <style?type="text/css"> #li1{ width:200px; height:100px; list-style:none; background-color:yellow; border:3px?solid?#000; opacity:0.3; filter:alpha(opacity:30); }? </style> <script?src="move.js"></script> <script?type="text/javascript"> window.onload=function(){ var?li=document.getElementById("li1"); li.onmouseover=function(){ startMove(li,{width:400,height:200}); }; }; </script> </head> <body> <ul> <li?id="li1"></li> </ul> </body> </html>
下面是js
function?startMove(obj,json,fn){ clearInterval(obj.timer); obj.timer=setInterval(function(){ for(var?attr?in?json){ var?temp=0; //取當(dāng)前值 if(attr=="opacity"){ temp=Math.round(parseFloat(getStyle(obj,attr))*100); }else{ temp=parseInt(getStyle(obj,attr)); } //計(jì)算速度 var?speed=(json[attr]-temp)/8; speed=speed>0?Math.ceil(speed):Math.floor(speed); //檢測(cè)停止 if(temp==json[attr]){ clearInterval(obj.timer); if(fn){ fn(); } }else{ if(attr=="opacity"){ obj.style.filter="alpha(opacity:"+(temp+speed)+")"; obj.style.opacity=(temp+speed)/100; }else{ obj.style[attr]=temp+speed+"px"; } } } },30); } function?getStyle(obj,attr){ if(obj.currentStyle){ return?obj.currentStyle[attr]; }else{ return?getComputedStyle(obj,false)[attr]; } }
為什么沒(méi)反應(yīng)呢??
2017-05-03
測(cè)試了一下可以用,看看是不是JS<script?src="move.js"></script>引用路徑有誤,另外需要加Flag來(lái)保證只有每個(gè)值都達(dá)到target才停止動(dòng)畫(huà),不然透明度到1寬度還沒(méi)到400就不動(dòng)了