Last active
September 8, 2017 17:49
-
-
Save kahl-dev/aabb77b3a82ce04d8ad99ba82a530b35 to your computer and use it in GitHub Desktop.
ES6 event to trigger once per scroll
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 onScrollOnce(fn, scope) { | |
let mw1 = 0, | |
mw2 = 0; | |
let lock; | |
return function() { | |
const context = scope || this; | |
const args = arguments; | |
const e = args[0]; | |
const d = e.deltaY; | |
if ( | |
((d > 0 && d < mw2 && mw2 > mw1) || (d < 0 && d > mw2 && mw2 < mw1)) && | |
!lock | |
) { | |
fn.apply(context, args); | |
lock = true; | |
window.setTimeout(function() { | |
lock = false; | |
}, 200); | |
} | |
mw1 = mw2; | |
mw2 = d; | |
}; | |
} | |
function onScroll(e) { | |
consonle.log(e); | |
} | |
document.addEventListener('wheel', onScrollOnce(onScroll)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment