鼠標離開之后為什么一直閃爍
<!doctype html>
<html>
<head>
<style>
*{margin:0;padding:0;}
#div1{width:200px;height: 200px;background: red;opacity:0.3;filter:alpha(opacity:30)};
</style>
<script type="text/javascript">
window.onload=function(){
var oDiv=document.getElementById("div1");
oDiv.onmouseover=function(){
startMove(1);
}
oDiv.onmouseout=function(){
startMove(0.3);
}
}
var int=null;
var alpha=0.3;
function startMove(iTarget){
var oDiv=document.getElementById("div1");
clearInterval(int);
int=setInterval(function(){
? ?var speed=0;
? ?if(alpha>iTarget)
? ?{
? ? speed=-0.1;
? ?}
? ?else
? ?{
? ? speed=0.1;
? ?}
if(alpha==iTarget)
{
clearInterval(int);
}
else
{
? ? ? ? ? ? alpha+=speed;
? ? ? ? ? ? oDiv.style.opacity=alpha;
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
2017-02-20
應該是你的值有問題,后面的課程有講到 ?取值的時候有問題,你打開頁面開發者工具發現最后的值是浮動變化的
2016-12-21
追問一下,if(alpha>iTarget)為什么不是>=,我都試了,兩個都行,但是只是大于的話,30>30是不成立的,速度應該變為正了