Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Comparing scroll position to element in scroll handler
import throttle from 'lodash.throttle'
function handleScroll () {
// get the updated scroll position
const yOffset = window.pageYOffset
// compare to the cached pixel value when we want to reveal the element
if (yOffset > this.scrollIntoViewThreshold) {
// now reveal the element!
}
}
// ensure we don't fire this handler too often
// for a good intro into throttling and debouncing, see:
// https://css-tricks.com/debouncing-throttling-explained-examples/
const throttledScrollHandler = throttle(handleScroll, 100)
// now re-check on scroll
window.addEventListener('scroll', throttledScrollHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.