-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (69 loc) · 2.54 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
// Function to check if the element is in the viewport
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
// Function to run on scroll
function runOnScroll() {
const elements = document.querySelectorAll('.emoji-people');
elements.forEach((el) => {
const inView = isInViewport(el);
const hasAnimate = el.classList.contains('animate');
if (inView && !hasAnimate) {
el.classList.add('animate');
} else if(!inView && hasAnimate){
el.classList.remove('animate');
}
});
}
async function emojiTransitions(emojiElement, displayTime){
const femaleEmojis = ["👩🏻⚕️", "👩🏼⚕️", "👩🏽⚕️", "👩🏾⚕️", "👩🏿⚕️"];
const maleEmojis = ["👨🏻⚕️", "👨🏼⚕️", "👨🏽⚕️", "👨🏾⚕️", "👨🏿⚕️"];
const originalEmojis = [...femaleEmojis, ...maleEmojis];
var activeEmojis = [];
while(true){
if(activeEmojis.length === 0){
activeEmojis = [...originalEmojis];
}
const fadeOutPromise = watchForEvent(emojiElement, "animationend");
emojiElement.classList.add("emoji-fade-out-class");
await fadeOutPromise;
emojiElement.classList.remove("emoji-fade-out-class");
emojiElement.style.opacity = 0;
const nextEmojiIndex = Math.floor(Math.random() * activeEmojis.length);
const nextEmoji = activeEmojis.splice(nextEmojiIndex, 1)[0];
emojiElement.textContent = nextEmoji;
const fadeIntPromise = watchForEvent(emojiElement, "animationend");
emojiElement.classList.add("emoji-fade-in-class");
await fadeIntPromise;
emojiElement.classList.remove("emoji-fade-in-class");
emojiElement.style.opacity = 1;
await delay(displayTime);
}
}
function delay(time){
return new Promise((res)=>{
setTimeout(res, time);
});
}
function watchForEvent(eventTarget, event){
var l;
return new Promise((res)=>{
eventTarget.addEventListener(event, l = (e)=>{
eventTarget.removeEventListener(event, l);
res(e);
});
});
}
emojiTransitions(
document.querySelector('#doctorEmoji'),
// 1000, // Time for fade transition
2000 // Time to display each emoji
);
// Listen for scroll events
window.addEventListener('scroll', runOnScroll);