JavaScript实现透明度的改变

鼠标的移入移出,显示的透明度在一定时间内发生的改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<html>
<head>
<title></title>
<meta charset="utf-8">
<style>
#div1{
width:200px;
height:200px;
background:red;
opacity:0.5;
</style>
<script>
window.onload=function(){
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function(){
startMove(30);
}
oDiv.onmouseout=function(){
startMove(100);
}
}
var timer=null;
var alpha=30;
function startMove(itarget){
clearInterval(timer);
var oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function(){
var speed=0;
if(alpha>itarget){
speed=-10;
}
else
{
speed=10;
}
if(alpha==itarget){
clearInterval(timer);
}
else{
alpha+=speed;
oDiv.style.opacity=alpha/100;
}
},30)
}
</script>
</head>
<body>
<div id ="div1">
</div>
</body>
</html>
Fork me on GitHub