-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
127 lines (108 loc) · 3.14 KB
/
script.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Bánkitó 2ö2ö
*
* Drop some pills and make some sparkles <3 <3 <3
*
* @author Dávid Godzsák (insta: @godzsaaa, @made.by.david)
*/
//CONST
var MIN_BUBBLE_AMOUNT = 5;
var EXTRA_BUBBLES = 30;
//GLOBAL VARS
var isScrolling = false;
var scrollBackCallBack = null;
var initialScrollEvent = true;
//UTIL
function classNameForEach(className, fn) {
var items = document.getElementsByClassName(className);
for (var i = 0; i < items.length; i++) {
fn(items[i])
}
}
function runLaterWith(f, i, t) {
setTimeout(function () {
f(i)
}, t);
}
//INITIALIZATION FN
function shuffleWiggle() {
classNameForEach("wiggle", function (it) {
it.style.animationDuration = (Math.random() * 2000 + 2000) + "ms";
});
}
function shuffleDrop() {
classNameForEach("pill", function (it) {
setTimeout(function () {
it.className += " drop"
}, Math.random() * 1400 + 100)
});
}
function makeBubble(bubble, now) {
var newBubble = bubble.cloneNode(true);
var size = Math.random() * 70 + 10;
newBubble.style.width = size + "%";
newBubble.children[0].style.animationDirection = Math.random() > 0.5 ? "alternate" : "alternate-reverse";
newBubble.style.left = Math.random() * 100 + "px";
newBubble.style.animationDuration = size * 15 + 1200 + "ms";
if (now) {
newBubble.style.animationDelay = 0 + "ms";
}
bubble.parentNode.appendChild(newBubble);
}
function turnOnBubbleBath() {
classNameForEach("bubbles", function (it) {
var bubble = it.children[0];
for (var j = 0; j < Math.random() * 20 + MIN_BUBBLE_AMOUNT; j++) {
runLaterWith(makeBubble, bubble, Math.random() * 4000)
}
})
}
function addBubbles(bubble, amount) {
for (var i = 0; i < amount; i++) {
runLaterWith(function () {
makeBubble(bubble, true);
}, null, Math.random() * 400);
}
}
function removeBubbles(bubble, amount) {
var children = bubble.parentNode.children;
var realAmount = amount > MIN_BUBBLE_AMOUNT ? amount : 0;
for (var i = 1; i < realAmount; i++) {
runLaterWith(function (a) {
a.remove()
}, children[i], Math.random() * 800);
}
}
function updateBubbleBath() {
classNameForEach("bubbles", function (it) {
var bubble = it.children[0];
var existingBubbles = it.children.length;
var numberOfBubbles = Math.round(Math.random() * 20 + MIN_BUBBLE_AMOUNT + (isScrolling ? EXTRA_BUBBLES : 0));
let bubbleDelta = existingBubbles - numberOfBubbles;
if (bubbleDelta > 0) {
removeBubbles(bubble, bubbleDelta);
} else {
addBubbles(bubble, -bubbleDelta);
}
})
}
//LISTENERS
window.addEventListener('scroll', function (e) {
if (initialScrollEvent) {
initialScrollEvent = false;
return;
}
if (!isScrolling) {
isScrolling = true;
updateBubbleBath();
}
clearTimeout(scrollBackCallBack);
scrollBackCallBack = setTimeout(function () {
isScrolling = false;
updateBubbleBath();
}, 600);
});
// MAIN
shuffleDrop();
shuffleWiggle();
turnOnBubbleBath();