-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssAnimations.js
More file actions
88 lines (85 loc) · 3.45 KB
/
cssAnimations.js
File metadata and controls
88 lines (85 loc) · 3.45 KB
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
const isNodeList = (nodes) => typeof nodes === 'object' && /^\[object (HTMLCollection|NodeList|Object)\]$/.test(Object.prototype.toString.call(nodes)) && (typeof nodes.length ==='number') && (nodes.length === 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0));
const css = {};
class cssFunction {
constructor(toChanges, changes, time) {
this.toChanges = toChanges;
this.changes = changes;
this.time = time;
}
run(element, then, thenArgs) {
let length = (isNodeList(element) || Array.isArray(element)) ? Object.keys(element).length : 1;
if (this.toChanges.setUp !== undefined) {
for (let i = 0; i < length; i++) {
let change = (isNodeList(element) || Array.isArray(element)) ? element[i] : element;
for (let j = 0; j < this.toChanges.setUp.length; j++) {
change.style[this.toChanges.setUp[j]] = this.changes.setUp[j](change);
}
}
}
let start = null;
const animate = (timeStamp) => {
if (!start) start = timeStamp;
let progress = timeStamp - start;
for (let i = 0; i < length; i++) {
let change = (isNodeList(element) || Array.isArray(element)) ? element[i] : element;
change.style[this.toChanges.animate] = this.changes.animate(progress, change);
}
if (progress < this.time) {
window.requestAnimationFrame(animate);
} else {
if (this.toChanges.end !== undefined) {
{
for (let i = 0; i < length; i++) {
let change = (isNodeList(element) || Array.isArray(element)) ? element[i] : element;
for (let j = 0; j < this.toChanges.end.length; j++) {
change.style[this.toChanges.end[j]] = this.changes.end[j](progress, change);
}
}
}
}
if (then !== undefined) {
(Array.isArray(then)) ? then[0].run(then[1], then.splice(0, 2)): then.run(thenArgs);
}
}
};
window.requestAnimationFrame(animate);
}
}
css.bounceDownUp = new cssFunction({
animate: "transform",
end: ["display", "top"]
}, {
animate: (progress) => `translateY(${-((progress/100)**2.5 - (progress/100)*10)}px)`,
end: [() => 'none', (progress, change) => `${change.style.top + (-((progress/100)**2.5 - (progress/100)*10))}px`]
},
2000
);
css.fadeIn = new cssFunction({
setUp: ["display"],
animate: "opacity"
}, {
setUp: [() => 'block'],
animate: (progress) => progress/300
},
300
);
css.fadeOut = new cssFunction({
animate: "opacity",
end: ["display"]
}, {
animate: (progress) => 1 - progress/300,
end: [() => 'none']
},
300
);
css.slideDownToMiddle = new cssFunction({
setUp: ["display", "top"],
animate: "transform",
end: ["top"]
}, {
setUp: [() => 'block', (progress, change) => `${-change.offsetHeight}px`],
animate: (progress) => `translateY(${-((progress/35 - (innerHeight/1.5 + 20)**.5)**2 - innerHeight/1.5 + 20)}px)`,
end: [(progress, change) => `${change.style.top + (-((progress/35 - (innerHeight/1.5 + 20)**.5)**2 - innerHeight/1.5 + 20))}px`]
},
1100
);