-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (84 loc) · 2.35 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" >
<meta charset="utf-8" >
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: #1d7db1;
opacity:0;
}
</style>
</head>
<body>
<div id="div1"></div>
<span id="span1">123</span>
<button>fadein</button>
<button>fadeOut</button>
<script>
(function(){
let fadeFlag = false;
function Fade(selector){
this.elem = typeof selector == "Object" ? selector : document.getElementById(selector);
}
Fade.prototype={
constructor: Fade,
setOpacity: (elem,opacity)=>{
elem.filters ? elem.style.filter = "alpha(opacity="+opacity+")" : elem.style.opacity =opacity / 100;
return true;
},
setOpacity:function(num){
this.elem.style.opacity !== undefined ? this.elem.style.opacity = num /100 : this.elem.style.filter = "alpha(opacity="+num+")";
},
fadeIn:function(speed,opacity){
speed = speed || 2;
opacity = opacity || 100;
let num = 0;
if(!fadeFlag){
let time = setInterval(()=>{
num += speed;
fadeFlag = false ;
this.setOpacity(num);
/*配合css样式处理-->>*/this.elem.style.opacity !== undefined ? this.elem.style.opacity = num/100 : this.elem.style.filter = "alpha(opacity="+num+")";
if(num >= opacity){
clearInterval(time);
fadeFlag = true;
}
},20);
}
},
fadeOut:function(speed,opacity){
speed = speed || 2;
opacity = opacity || 0;
let num = 100;
if(fadeFlag){
let time = setInterval(()=>{
num -= speed;
fadeFlag = true;
this.setOpacity(num);
this.elem.style.opacity !== undefined ? this.elem.style.opacity = num / 100 : this.elem.style.filter = "alpha(opacity="+num+")";
if(num <= opacity){
clearInterval(time);
fadeFlag = false;
}
},20);
}
}
}
window.Fade = Fade;
})();
var btn = document.getElementsByTagName("button")[0];
var btn2 = document.getElementsByTagName("button")[1];
btn.onclick = ()=>{
let fade = new Fade("div1");
fade.fadeIn();
};
btn2.onclick = ()=>{
let fade = new Fade("div1");
fade.fadeOut();
}
</script>
</body>
</html>