From 70e7cf02585589448e17752063d5006f4bb7a14f Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Thu, 25 Aug 2016 11:28:29 -0700 Subject: [PATCH 1/4] cancel the jump animation --- src/jump.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/jump.js b/src/jump.js index 9e59114..abeed48 100644 --- a/src/jump.js +++ b/src/jump.js @@ -23,6 +23,8 @@ const jumper = () => { let callback // to call when done scrolling (function) + let requestID // requestAnimationFrame id (number) + // scroll position helper function location() { @@ -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 @@ -82,6 +88,12 @@ const jumper = () => { timeStart = false } + // cancels the requestAnimationFrame + + function cancel() { + cancelAnimationFrame(requestID); + } + // API function jump(target, options = {}) { @@ -136,7 +148,9 @@ const jumper = () => { } // start the loop - requestAnimationFrame(loop) + requestID = requestAnimationFrame(loop) + + return cancel } // expose only the jump method From 60303346e9f7553cafd57ac86070970af79e4b2a Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Tue, 30 Aug 2016 14:22:54 -0700 Subject: [PATCH 2/4] return the cancel jump --- src/jump.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/jump.js b/src/jump.js index abeed48..15e5660 100644 --- a/src/jump.js +++ b/src/jump.js @@ -154,7 +154,10 @@ const jumper = () => { } // expose only the jump method - return jump + return { + jump, + cancel, + }; } // export singleton From b72c2fa37f8811515f5910067f697d3be6df1c98 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Tue, 30 Aug 2016 15:30:21 -0700 Subject: [PATCH 3/4] restart the jump start --- src/jump.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/jump.js b/src/jump.js index 15e5660..f2c1fc6 100644 --- a/src/jump.js +++ b/src/jump.js @@ -91,6 +91,8 @@ const jumper = () => { // cancels the requestAnimationFrame function cancel() { + timeStart = false; + cancelAnimationFrame(requestID); } From e8f5e69f23f3908578909e10ac1066e356ef8425 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Thu, 1 Sep 2016 17:08:39 -0700 Subject: [PATCH 4/4] expose whether the jumper is running --- src/jump.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/jump.js b/src/jump.js index f2c1fc6..fb13bd0 100644 --- a/src/jump.js +++ b/src/jump.js @@ -96,6 +96,12 @@ const jumper = () => { cancelAnimationFrame(requestID); } + // indicates whether jumper is executing + + function isJumping() { + return !!timeStart; + } + // API function jump(target, options = {}) { @@ -159,6 +165,7 @@ const jumper = () => { return { jump, cancel, + isJumping, }; }