-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_up.js
73 lines (66 loc) · 2.23 KB
/
count_up.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
let section_counter = document.querySelector('#section_counter');
let counters = document.querySelectorAll('.counter-item .counter');
// Scroll Animation
let CounterObserver = new IntersectionObserver(
(entries, observer) => {
let [entry] = entries;
if (!entry.isIntersecting) return;
let speed = 200;
counters.forEach((counter, index) => {
function UpdateCounter() {
const targetNumber = +counter.dataset.target;
const initialNumber = +counter.innerText;
const incPerCount = targetNumber / speed;
if (initialNumber < targetNumber) {
counter.innerText = Math.ceil(initialNumber + incPerCount);
setTimeout(UpdateCounter, 5);
}
}
UpdateCounter();
if (counter.parentElement.style.animation) {
counter.parentElement.style.animation = '';
} else {
counter.parentElement.style.animation = `slide-up 0.3s ease forwards ${
index / counters.length + 0.5
}s`;
}
});
observer.unobserve(section_counter);
},
{
root: null,
threshold: window.innerWidth > 768 ? 0.4 : 0.3,
}
);
CounterObserver.observe(section_counter);
function playPauseVideo() {
let videos = document.querySelectorAll("video");
videos.forEach((video) => {
// We can only control playback without insteraction if video is mute
video.muted = true;
// Play is a promise so we need to check we have it
let playPromise = video.play();
if (playPromise !== undefined) {
playPromise.then((_) => {
let observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (
entry.intersectionRatio !== 1 &&
!video.paused
) {
video.pause();
} else if (video.paused) {
video.play();
}
});
},
{ threshold: 0.2 }
);
observer.observe(video);
});
}
});
}
// And you would kick this off where appropriate with:
playPauseVideo();