慕仰4907821
2016-01-22 20:28
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#div1{
width:200px;
height:200px;
background:red;
filter:alpha(opacity:30); /*支持Firefox, Safari, Chrome*/
opacity:0.3; /*支持IE*/
}
</style>
<script>
window.onload=function(){
var oDiv=document.getElementById("div1");
oDiv.onmouseover=function(){
startMove(100);
}
oDiv.onmouseout=function(){
startMove(30);
}
}
var timer=null;
var alpha=30;
var oDiv=document.getElementById("div1");
function startMove(target){
clearInterval(timer);
timer=setInterval(function(){
var speed=0;
if(alpha>target){
speed=-10;
}else{
speed=10;
}
if(alpha==target){
clearInterval(timer);
}else{
alpha+=speed;
oDiv.style.filter='alpha(opacity:'+alpha+')';
oDiv.opacity=alpha/100;
}
},30)
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
(1)var oDiv=document.getElementById("div1");把这句放在startMove函数里面;
(2) oDiv.opacity=alpha/100;这一句写少了style
JS动画效果
113925 学习 · 1443 问题
相似问题