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

cancel the jump animation #44

Open
wants to merge 4 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: 31 additions & 5 deletions src/jump.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const jumper = () => {

let callback // to call when done scrolling (function)

let requestID // requestAnimationFrame id (number)

// scroll position helper

function location() {
Expand Down Expand Up @@ -53,9 +55,13 @@ const jumper = () => {
window.scrollTo(0, next)

// check progress
timeElapsed < duration
? requestAnimationFrame(loop) // continue scroll loop
: done() // scrolling is done
if (timeElapsed < duration) {
// continue scroll loop
requestID = requestAnimationFrame(loop)
} else {
// scrolling is done
done();
}
}

// scroll finished helper
Expand All @@ -82,6 +88,20 @@ const jumper = () => {
timeStart = false
}

// cancels the requestAnimationFrame

function cancel() {
timeStart = false;

cancelAnimationFrame(requestID);
}

// indicates whether jumper is executing

function isJumping() {
return !!timeStart;
}

// API

function jump(target, options = {}) {
Expand Down Expand Up @@ -136,11 +156,17 @@ const jumper = () => {
}

// start the loop
requestAnimationFrame(loop)
requestID = requestAnimationFrame(loop)

return cancel
}

// expose only the jump method
return jump
return {
jump,
cancel,
isJumping,
};
}

// export singleton
Expand Down