Skip to content

Instantly share code, notes, and snippets.

@awesomephant
Last active January 24, 2019 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesomephant/07391b0eb018769b93e097966a995be5 to your computer and use it in GitHub Desktop.
Save awesomephant/07391b0eb018769b93e097966a995be5 to your computer and use it in GitHub Desktop.
Mousemove Optimized
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