Skip to content

Instantly share code, notes, and snippets.

@weird-coon
Created September 29, 2020 09:50
Show Gist options
  • Save weird-coon/de8707d72eae521c1f1e0304e8650836 to your computer and use it in GitHub Desktop.
Save weird-coon/de8707d72eae521c1f1e0304e8650836 to your computer and use it in GitHub Desktop.
add animate class when element in viewport
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