Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellyrmilligan/aba9dd155ed77dc579faa8d022ec65a6 to your computer and use it in GitHub Desktop.
Save kellyrmilligan/aba9dd155ed77dc579faa8d022ec65a6 to your computer and use it in GitHub Desktop.
scroll cache module and raf loop
export default {
scrollY: '',
getScrollY() {
return (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
},
init() {
window.addEventListener('scroll', (e) => {
this.scrollY = this.getScrollY();
});
this.scrollY = this.getScrollY();
}
}
function componentInViewport(ts) {
let rootNode = self.getDOMNode();
if(isOnScreen(rootNode, 600)) {
self.setState({
componentInViewport: true
});
if (self.onComponentInViewport) {
self.onComponentInViewport();
}
window.cancelAnimationFrame(self.animationFrameId);
} else {
self.animationFrameId = window.requestAnimationFrame(componentInViewport);
}
}
this.animationFrameId = window.requestAnimationFrame(componentInViewport);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment