Skip to content

Instantly share code, notes, and snippets.

@zlwaterfield
Last active October 26, 2019 22:17
Show Gist options
  • Save zlwaterfield/8f4ce8131b1570daec3f58bb2bfd7b27 to your computer and use it in GitHub Desktop.
Save zlwaterfield/8f4ce8131b1570daec3f58bb2bfd7b27 to your computer and use it in GitHub Desktop.
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