-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (47 loc) · 1.92 KB
/
index.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
import('https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.7.8/lottie.min.js').then(module => {
let lottieArr = document.querySelectorAll('sketro-animation');
Array.from(lottieArr).map((dataEl,i)=>{
let lottieId = dataEl.getAttribute('id').split('-')[1];
let lottieEl = document.querySelector(`#sketro-${lottieId}`);
let iconMenu = lottie.loadAnimation({
container: lottieEl, // the dom element that will contain the animation
renderer: 'svg',
loop: false,
autoplay: false,
path: `https://sketchdbmin.herokuapp.com/lottieCode/?id=${lottieId}` // the path to the animation json
});
let played = false;
window.addEventListener('scroll',()=>{
if (!played) {
if (isInViewport(lottieEl)) {
played = true;
playAndLoop(iconMenu)
}
}
})
iconMenu.addEventListener('data_ready',()=> {
if (isInViewport(lottieEl) ) {
if (!played) {
played = true;
playAndLoop(iconMenu)
}
}
});
})
function isInViewport(el) {
let percentVisible = 80;
let
rect = el.getBoundingClientRect(),
windowHeight = (window.innerHeight || document.documentElement.clientHeight);
return !(
Math.floor(100 - (((rect.top >= 0 ? 0 : rect.top) / +-rect.height) * 100)) < percentVisible ||
Math.floor(100 - ((rect.bottom - windowHeight) / rect.height) * 100) < percentVisible
)
};
function playAndLoop(el) {
el.playSegments([-30, 120], true);
el.addEventListener('complete',()=>{
el.playSegments([0, 120], true)
});
}
});