Created
November 4, 2012 01:21
-
-
Save bloodyowl/4009702 to your computer and use it in GitHub Desktop.
betterScrollEvent
This file contains hidden or 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
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) |
This file contains hidden or 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
var root = DOM(window) | |
, isScrollingMethod2 = false | |
root.listen("scroll", function(){ | |
isScrollingMethod2 = true | |
}) | |
;(function(){ | |
if(isScrollingMethod2) // callback | |
isScrollingMethod2 = false | |
}).every(.03) |
This file contains hidden or 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
DOM(window).listen("scroll", function(){ | |
// callback | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.