Skip to content

Instantly share code, notes, and snippets.

@pocketjoso
Last active August 9, 2017 13:24
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 pocketjoso/2cba26da4b23b83d6f3ecca81f997edc to your computer and use it in GitHub Desktop.
Save pocketjoso/2cba26da4b23b83d6f3ecca81f997edc to your computer and use it in GitHub Desktop.
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