1 <body> 2 <input type="button" id="btn" value="启动"/> 3 <img src="timg.jpg" id="img" alt="" /> 4 <script type="text/javascript"> 5 6 window.onload = function() 7 { 8 var time = null; 9 var img = document.getElementById("img"); 10 11 img.onmouseover = function() 12 { 13 startMove(img,100,10); 14 15 //alert(getComputedStyle(img,false).opacity) 16 17 } 18 img.onmouseout = function() 19 { 20 startMove(img,30,-10); 21 } 22 23 function startMove(obj,iTarget,ispeed) 24 { 25 var icur =0; 26 clearInterval(time); 27 time = setInterval(function(){ 28 icur = Math.round((getComputedStyle(img,false).opacity)*100); //为什么不能获取到呢 29 if(icur == iTarget) 30 { 31 clearInterval(time); 32 }else{ 33 obj.style.opacity = (icur + ispeed)/100 ; 34 obj.style.filter = 'alpha(opacity = '+(icur + ispeed)+')'; 35 } 36 },30) 37 } 38 } 39 40 </script> 41 </body>
注意 ie下不支持
getComputedStyle