鼠標(biāo)移出后沒有反應(yīng)
?
<!doctype html>
<html>
<head>
?<meta charset="UTF-8">
?<title>透明度</title>
?<style type="text/css">
?*{margin:0;padding: 0;}
????? #div1{
????? ?background: red;
????? ?height:200px;
????? ?width:200px;
????? ?left:0px;
????? ?top:80px;
??????? opacity:0.3;
??????? filter:alpha(opacity:30);//IE
????? }
????
?</style>
<script type="text/javascript">
???? window.onload=function(){
?????? var oDiv=document.getElementById('div1');
?????? oDiv.onmouseover=function(){
???????? startmove(100);
?????? }
?????? oDiv.onmouseout=function(){
???????? startmove(30);
?????? }
???? }
???? var timer=null;
????
???? function startmove(goal){
?????? clearInterval(timer);
?????? var oDiv=document.getElementById('div1');
?????? var alpha=30;
??????? var speed=0;
?????? timer=setInterval(function(){
????
?????? ?if(alpha==goal){???? //判斷加在定時(shí)器里面
?????? ?clearInterval(timer);
?????? }
?????? else{
??????? if(goal>alpha){
????????? speed=1;
??????? }
??????? else{speed=-1;}
??????? alpha+=speed;
??????? oDiv.style.opacity=alpha/100;
??????? oDiv.style.filter='alpha(opacity:'+alpha+')';//IE
?????? }?
?????????? },30)
??????
???? }
???
</script>
</head>
<body>
?<div id="div1"></div>
?
</body>
</html>
2017-04-06
明白了,把var alpha=30移出去就可以啦