Created
January 7, 2019 14:04
-
-
Save dan-gamble/9d04763595c3930dceab22a9c18bee77 to your computer and use it in GitHub Desktop.
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
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