From 78edd06d150f82c376cf14d0a465fc8e863f78d6 Mon Sep 17 00:00:00 2001 From: Mordechai Meisels Date: Thu, 7 May 2020 12:04:20 -0400 Subject: [PATCH] Fix #28 --- src/lib/ScrollspyNav.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib/ScrollspyNav.js b/src/lib/ScrollspyNav.js index c1a94f3..a5b4162 100644 --- a/src/lib/ScrollspyNav.js +++ b/src/lib/ScrollspyNav.js @@ -15,6 +15,7 @@ class ScrollspyNav extends Component { this.scrollDuration = Number(this.props.scrollDuration) || 1000; this.headerBackground = this.props.headerBackground === "true" ? true : false; this.offset = this.props.offset || 0; + this.scrollDestination = 0; this.onScroll = this.onScroll.bind(this); @@ -69,11 +70,20 @@ class ScrollspyNav extends Component { * @param {Number} duration */ scrollTo(start, to, duration) { + this.scrollDestination = to; let change = to - start, currentTime = 0, increment = 10; let animateScroll = () => { + /* + * Stop previous animation when destination changes + * + * https://github.com/StephenWeiXu/react-scrollspy-nav/issues/28 + */ + if (this.scrollDestination !== to) + return; + currentTime += increment; let val = this.easeInOutQuad(currentTime, start, change, duration); window.scrollTo(0, val); @@ -140,6 +150,7 @@ class ScrollspyNav extends Component { }); }) + this.onScroll(); window.addEventListener("scroll", this.onScroll); }