Created
June 29, 2016 10:45
-
-
Save amio/134bd2300d1ef1dc721225c439e4a299 to your computer and use it in GitHub Desktop.
Throttled requestAnimationFrame
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
// Usage: | |
// | |
// var onScroll = traf(function () { | |
// item[0].style.width = window.scrollY + 100 + 'px'; | |
// }) | |
// window.addEventListener('scroll', onScroll, false); | |
export default function traf (fn) { | |
var ticking = false; | |
return function () { | |
if(!ticking) { | |
ticking = true; | |
requestAnimationFrame(function () { | |
ticking = false; | |
fn() | |
}); | |
} | |
} | |
} |
Author
amio
commented
Jun 29, 2016
- https://css-tricks.com/debouncing-throttling-explained-examples/
- http://codepen.io/dcorb/pen/pgOKKw
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment