慕田峪6997291
2017-04-06 18:43
<!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){ //判断加在定时器里面
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>
明白了,把var alpha=30移出去就可以啦
JS动画效果
113925 学习 · 1443 问题
相似问题