Skip to content

Instantly share code, notes, and snippets.

@dan-gamble
Created January 7, 2019 14:04
Show Gist options
  • Save dan-gamble/9d04763595c3930dceab22a9c18bee77 to your computer and use it in GitHub Desktop.
Save dan-gamble/9d04763595c3930dceab22a9c18bee77 to your computer and use it in GitHub Desktop.
export default class OnScrollSixTFPS {
constructor () {
this.isTicking = false
this.lastKnownY = 0
this.onScroll = this.onScroll.bind(this)
this.updateEl = this.updateEl.bind(this)
this.setupListeners()
}
setupListeners () {
window.addEventListener('scroll', this.onScroll)
}
onScroll () {
this.lastKnownY = window.scrollY
this.requestTick()
}
requestTick () {
if (!this.isTicking) window.requestAnimationFrame(this.updateEl)
// If a rAF is already queued we don't want to call another one
this.isTicking = true
}
updateEl () {
// Reset the tick so we can capture the next onScroll
this.isTicking = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment