<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>opacity</title> <style type="text/css"> *{ margin: 0; padding: 0; } #oDiv { width: 200px; height: 200px; background-color: blue; } </style> </head> <body> <div id="oDiv"></div> <script type="text/javascript"> window.onload = function(){ var oDiv = document.getElementById('oDiv'); oDiv.onmouseover = function(){ startmove(0.5); } oDiv.onmouseout = function(){ startmove(1); } var timer = null; function startmove(target){ clearInterval(timer); timer = setInterval(function(){ var speed = 0; if(oDiv.style.opacity>target){ speed = -0.1; }else{ speed = 0.1; }; if (oDiv.style.opacity==target) { clearInterval(timer); } else { oDiv.style.opacity+=speed; } },50); } } </script> </body> </html>
求解,设置鼠标放置和拿开是透明度渐变和复原;
上面代码中有哪些错误需要更正?
还有oDiv.style.opacity==target这句是否能比较?
相关分类