Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopping scroll if user or other system scrolls screen mid-smooth scroll #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/smoothscroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
return el;
}

var lastStartX,
lastStartY,
lastCurrentX,
lastCurrentY;
/**
* self invoked function that, given a context, steps through scrolling
* @method step
Expand All @@ -122,10 +126,42 @@
currentX = context.startX + (context.x - context.startX) * value;
currentY = context.startY + (context.y - context.startY) * value;

//The user, or some other system, scrolled mid scroll.
if(typeof lastCurrentX != "undefined" &&
typeof lastCurrentY != "undefined" &&
( document.body.scrollTop !== Math.floor(lastCurrentY)
|| document.body.scrollLeft !== Math.floor(lastCurrentX))){
lastStartX = undefined;
lastStartY = undefined;
lastCurrentX = undefined;
lastCurrentY = undefined;
w.cancelAnimationFrame(context.frame);
return;
}

//The user, or some other system, scrolled before the scroll began.
if(typeof lastStartX != "undefined" && typeof lastStartY != "undefined" && (context.startX !== lastStartX || context.startY !== lastStartY)){
lastStartX = undefined;
lastStartY = undefined;
lastCurrentX = undefined;
lastCurrentY = undefined;
w.cancelAnimationFrame(context.frame);
return;
}

context.method.call(context.scrollable, currentX, currentY);

lastStartX = context.startX;
lastStartY = context.startY;
lastCurrentX = currentX;
lastCurrentY = currentY

// return when end points have been reached
if (currentX === context.x && currentY === context.y) {
lastStartX = undefined;
lastStartY = undefined;
lastCurrentX = undefined;
lastCurrentY = undefined;
w.cancelAnimationFrame(context.frame);
return;
}
Expand Down