Created
September 29, 2020 09:50
-
-
Save weird-coon/de8707d72eae521c1f1e0304e8650836 to your computer and use it in GitHub Desktop.
add animate class when element in viewport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animateClass() { | |
const scroll = window.requestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60) } | |
const elementsToShow = document.querySelectorAll('.animate') | |
const _self = this | |
Array.prototype.forEach.call(elementsToShow, function(element) { | |
if (_self.isElementInViewport(element)) { | |
element.classList.add('is-visible') | |
} | |
}) | |
scroll(this.animateClass) | |
}, | |
isElementInViewport(el) { | |
const rect = el.getBoundingClientRect() | |
return ( | |
(rect.top <= 0 && rect.bottom >= 0) || | |
( | |
rect.bottom >= (window.innerHeight || document.documentElement.clientHeight) && | |
rect.top <= (window.innerHeight || document.documentElement.clientHeight) | |
) || (rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment