Last active
January 24, 2019 16:30
-
-
Save awesomephant/07391b0eb018769b93e097966a995be5 to your computer and use it in GitHub Desktop.
Mousemove Optimized
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 last_known_scroll_position = 0; | |
var ticking = false; | |
function doSomething(scroll_pos) { | |
// do something with the scroll position | |
} | |
window.addEventListener('mousemove', function(e) { | |
last_known_position = { | |
x: e.clientX, | |
y: e.clientY | |
}; | |
if (!ticking) { | |
window.requestAnimationFrame(function() { | |
doSomething(last_known_position); | |
ticking = false; | |
}); | |
ticking = true; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment