-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfade.js
92 lines (69 loc) · 2.16 KB
/
fade.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
88
89
90
91
92
var defaults = require("./defaults.js");
var utils = require("./utils.js");
function fade (element, start, end, duration, then) {
function update (v) {
element.style.opacity = utils.easing.swing(v);
}
utils.tween(start, end, update, duration, then);
}
function isVisible (element) {
if (utils.isHidden(element)) {
return false;
}
return !utils.isOpacityZero(element);
}
function fadeEffect (type, element, duration, then) {
var start = utils.getOpacity(element);
var end = type === "in" ? 1 : 0;
if (typeof duration === "function") {
then = duration;
duration = defaults.duration;
}
else if (typeof duration !== "number") {
duration = defaults.duration;
}
duration = utils.ensureIsPositive(duration);
return fade(element, start, end, duration, after);
function after () {
if (type === "out") {
element.style.display = "none";
}
if (typeof then === "function") {
then();
}
}
}
function fadeIn (element, duration, then) {
var ownDisplay = element.style.display;
//
// If the element's own value for `display` is `none`, we have to remove
// this value; if after that the element is still set to `none` for its
// computed values, it means that `none` was set in CSS and we have to
// assume this is a block element. Setting `display` to `""` in this case
// wouldn't accomplish anything.
//
if (ownDisplay === "none") {
element.style.opacity = "0";
element.style.display = "";
}
if (window.getComputedStyle(element).display === "none") {
element.style.opacity = "0";
element.style.display = "block";
}
return fadeEffect("in", element, duration, then);
}
function fadeOut (element, duration, then) {
return fadeEffect("out", element, duration, then);
}
function toggle (element, duration, then) {
return (
isVisible(element) ?
fadeOut(element, duration, then) :
fadeIn(element, duration, then)
);
}
module.exports = {
in: fadeIn,
out: fadeOut,
toggle: toggle
};