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