Skip to content

Instantly share code, notes, and snippets.

@wesruv
Created August 30, 2015 21:05
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 wesruv/e36375dc6c107878e4bc to your computer and use it in GitHub Desktop.
Save wesruv/e36375dc6c107878e4bc to your computer and use it in GitHub Desktop.
revealHandleMouseScroll: function(e) {
var now = new Date().getTime();
var revealLastRun = this.state.revealLastRun;
var revealLastScrollEvent = now - this.state.revealLastScrollEvent;
var revealLastScrollEventStrength = this.state.revealLastScrollEventStrength;
// Scroll direction is from sitepoint.com/html5-javascript-mouse-wheel/
var scrollDirection = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
var scrollDistance = null;
var scrollStrength = null;
var hasInertialScroll = false;
var activateRevealScroll = false;
e.preventDefault();
// Account for stupid browsers not having consistency in how they measure scroll
if (e.deltaY !== undefined) {
scrollDistance = e.deltaY;
} else if (e.wheelDelta !== undefined) {
scrollDistance = e.wheelDelta * -1;
}
if (scrollDistance !== null) {
scrollStrength = scrollDistance * -1 * scrollDirection;
}
// Try to determine if we're dealing with inertial scroll
if (scrollStrength !== null) {
hasInertialScroll = (scrollStrength !== revealLastScrollEventStrength || scrollStrength < 20) && revealLastScrollEvent < 50;
}
if (hasInertialScroll === true && now - revealLastRun > this.state.revealSettings.throttle) {
// Only run if the user put in more input, not if we're riding the tail of an old input
activateRevealScroll = scrollStrength > revealLastScrollEventStrength;
} else if (now - revealLastRun > this.state.revealSettings.throttle) {
activateRevealScroll = true;
}
// Set the time of the last scroll event
this.setState({
revealLastScrollEvent: new Date().getTime(),
revealLastScrollEventStrength: scrollStrength
});
if (activateRevealScroll) {
// revealScroll() will set revealScrollLastRun
if (scrollDirection < 0) {
this.revealScroll(1);
} else if (scrollDirection > 0) {
this.revealScroll(-1);
}
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment