(38)JS运动之淡入淡出
2017-04-17 19:05 tlnshuju 阅读(209) 评论(0) 收藏 举报基本思路:使用样式filter。可是要区分IE浏览器和chrom、firefox的不同,详细也是用定时器来实现。
<!DOCTYPE HTML>
<!--
-->
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#div1{
width:200px;
height:200px;
background:red;
filter:alpha(opacity:30);opacity:0.3;
}
</style>
<script>
window.onload=function ()
{
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function ()
{
startMove(100);
};
oDiv.onmouseout=function ()
{
startMove(30);
};
};
var alpha=30;//透明度变量,由于样式那里初始是30
var timer=null;
function startMove(iTarget){
var oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function (){
var speed=0;
if(alpha<iTarget)
{
speed=1;
}
else{
speed=-1;
}
if(alpha==iTarget)
{
clearInterval(timer);
}
else
{
alpha+=speed;
oDiv.style.filter='alpha(opacity:'+alpha+')';//IE的透明度
oDiv.style.opacity=alpha/100;//谷歌、火狐浏览器。满的值是1,不是100。因此除以100
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
效果图:
浙公网安备 33010602011771号