<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>透明度动画</title>
<style>
*{
padding: 0;
margin: 0;
}
.block{
width: 80px;
height: 80px;
filter:alpha(30);
opacity: 0.3;
background: #f00;
}
</style>
</head>
<body>
<div class="block"></div>
<script>
window.onload = function() {
var oDiv = document.getElementById('block');
oDiv.onmouseover=function(){
startMove(100);
};
oDiv.onmouseout=function(){
startMove(30);
};
};
var speed = 0;
var alpha = 30;
var timer = null;
function startMove(iTarget){
var oDiv = document.getElementById('block');
clearInterval(timer);
timer = setInterval(function(){
if(oDiv.alpha > iTarget){
speed = -10;
}else{
speed = 10;
}
if(oDiv.alpha == iTarget){
clearInterval(timer);
}else{
alpha += speed;
oDiv.style.filter = 'alpha(opacity:'+alpha+')'; // 兼容IE
oDiv.style.opacity = alpha/100; // Firefox Chrome
}
},30);
}
</script>
</body>
</html>