程序思路:
实际上是一种运动。跟运动一样的是,由一个透明度,运动到另一个透明度。
步骤:
1:写一个Div为其赋值为半透明,兼容FF和IE的兼容,这样写css:
opacity:0.3;filter:alpha(opacity=30);
2:写一个onload,获取元素,并为元素添加onmouseover和onmouseout事件。
3:用匀速运动框架,或者缓冲运动框架,写运动函数startMove(target)。当然缓冲运动更加自然一点。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#test{width:300px; height:300px; background:red; opacity:0.3; filter:alpha(opacity=30)}
</style>
<script>
var timer=null;
var speed=null;
var alpha=30;
window.οnlοad=function(){
var oDiv=document.getElementById("test");
oDiv.οnmοuseοver=function(){
startMove(100);
}
oDiv.οnmοuseοut=function(){
startMove(30);
}
}
function startMove(target){
var oDiv=document.getElementById("test");
clearInterval(timer);
speed=target-alpha>0?Math.ceil((target-alpha)/10):Math.floor((target-alpha)/10);
timer=setInterval(function(){
if(alpha==target){//停止条件
clearInterval(timer);
}
else{
alpha+=speed;//运动细节
oDiv.style.opacity=alpha/100;
oDiv.style.filter="alpha(opacity:"+alpha+")";//由于不能直接获得alpha只能用变量赋值了。
}
},30);
}
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>