Last active
October 26, 2019 22:17
-
-
Save zlwaterfield/8f4ce8131b1570daec3f58bb2bfd7b27 to your computer and use it in GitHub Desktop.
Blog post snippet - https://medium.com/gofaros/dynamic-and-responsive-svg-3de87f757e0d
This file contains 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
scrollReveal(elem) { | |
const className = 'is-visible'; | |
const ratio = .9; | |
const isElementInViewport = (el) => { | |
let rect = el.getBoundingClientRect(); | |
return ( | |
rect.top > 0 && | |
rect.top <= window.innerHeight * ratio | |
); | |
}; | |
const onVisibilityChange = (el, callback) => { | |
let oldVisible; | |
return () => { | |
if (el.classList.contains(className)) return; | |
let visible = isElementInViewport(el); | |
if (visible && visible != oldVisible) { | |
oldVisible = visible; | |
if (typeof callback == 'function') callback(); | |
} | |
}; | |
}; | |
let handler = onVisibilityChange(el, () => { | |
el.addClass(className); | |
}); | |
// Call this handler on document ready/load, on window resize, | |
// and on the element scroll | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment