求解:鼠标离开或者划过的时候,Div一直在闪?根本停不下来
<html>
<head>
<style type="text/css">
body,div{
margin:0;
padding:0;
}
div{
width:200px;
height:200px;
margin:10px;
float:left;
background:red;
opacity:0.3;
}
</style>
<script>
window.onload=function(){
var oDiv=document.getElementsByTagName("div");
for(var i=0;i<oDiv.length;i++){
oDiv[i].ialpha=0.3;
oDiv[i].onmouseover=function(){
startMove(this,1);
}
oDiv[i].onmouseout=function(){
startMove(this,0.3);
}
}
}
//var timer=null;
//var ialpha=0.3;
function startMove(obj,iTarget){
//var oDiv=document.getElementById("div1");
clearInterval(obj.timer);
timer=setInterval(function(){
var speed=0;
if(obj.ialpha>iTarget){
speed=-0.1;
}else if(obj.ialpha<iTarget){
speed=0.1;
}
if(obj.ialpha==iTarget){
clearInterval(obj.timer);
}else{
obj.ialpha+=speed;
obj.style.opacity=obj.ialpha;
}
},1000)
}
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
</body>
</html>
firefox偶尔ialpha=0.9999999999999999这种
chrome中 opacity=09/1/1.1;opacity=02/0.3/0.4一直在变化
(fedora)