Created
July 29, 2015 22:39
-
-
Save tylerthebuildor/c874995c82258d3f5cb9 to your computer and use it in GitHub Desktop.
The JavaScript scroll event isn't very peformant this MDN snippet fixes that with a modified event.
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
(function() { | |
'use strict'; | |
var throttle = function(type, name, obj) { | |
obj = obj || window; | |
var running = false; | |
var func = function() { | |
if (running) { return; } | |
running = true; | |
obj.requestAnimationFrame(function() { | |
obj.dispatchEvent(new CustomEvent(name)); | |
running = false; | |
}); | |
}; | |
obj.addEventListener(type, func); | |
}; | |
/* init - you can init any event */ | |
throttle ('scroll', 'optimizedScroll'); | |
})(); | |
// handle event | |
window.addEventListener('optimizedScroll', function() { | |
'use strict'; | |
console.log('Resource conscious scroll callback!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment