/
animation.js
36 lines (32 loc) · 1018 Bytes
/
animation.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
const debounce = function(func, wait, immediate) {
let timeout;
return function(...args) {
const context = this;
const later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
const target = document.querySelectorAll('[data-animation]');
const animationClass = 'animate';
function animeScroll() {
const windowTop = window.pageYOffset + ((window.innerHeight * 3) / 4);
target.forEach(function(element) {
if((windowTop) > element.offsetTop) {
element.classList.add(animationClass);
} else {
element.classList.remove(animationClass);
}
})
}
animeScroll();
if(target.length) {
window.addEventListener('scroll', debounce(function() {
animeScroll();
}, 100));
}