Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created November 4, 2012 01:21
Show Gist options
  • Save bloodyowl/4009702 to your computer and use it in GitHub Desktop.
Save bloodyowl/4009702 to your computer and use it in GitHub Desktop.
betterScrollEvent
var root = DOM(window)
, isScrolling = false
, interval
function getScrollState() { // verifies if window is still scrolling
if(!isScrolling) isScrolling = true
}
function getScrollStart() { // targets scrollStart and stops immediately
root.stopListening("scroll", getScrollStart).listen("scroll", getScrollState)
interval = (function () {
if(!isScrolling) { // if scroll is stopped, target start again and stop listening at scrollState
root.stopListening("scroll", getScrollState).listen("scroll", getScrollStart)
window.clearInterval(interval) // stops interval
return
} else {
isScrolling = false
// callback
}
}).every(.03) // slows down scrollEvent firing speed
}
root.listen("scroll", getScrollStart)
var root = DOM(window)
, isScrollingMethod2 = false
root.listen("scroll", function(){
isScrollingMethod2 = true
})
;(function(){
if(isScrollingMethod2) // callback
isScrollingMethod2 = false
}).every(.03)
DOM(window).listen("scroll", function(){
// callback
})
@bloodyowl
Copy link
Author

This little script slows down the firing speed on scroll event, and if no scroll is detected, the setInterval that handles restraint firing is stopped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment