From c88bab21c17e20d2c61c012033db51b3ba90c6e3 Mon Sep 17 00:00:00 2001 From: cesc fabregas Date: Fri, 12 Jul 2019 09:49:10 +0800 Subject: [PATCH] [bugfix] IndexBar: activeAnchor bug fixes (#3832) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复 sticky 设为 false 时activeAnchor失效的bug; 修复 this.scroller 不为 window 时activeAnchor错乱的bug; --- src/index-bar/index.js | 35 ++++++++++--------- .../test/__snapshots__/index.spec.js.snap | 2 +- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/index-bar/index.js b/src/index-bar/index.js index b68efd57f..a6be6c7a6 100644 --- a/src/index-bar/index.js +++ b/src/index-bar/index.js @@ -77,11 +77,9 @@ export default createComponent({ methods: { onScroll() { - if (!this.sticky) { - return; - } - - const scrollTop = getScrollTop(this.scroller); + const scrollTop = this.scroller === window + ? getScrollTop(this.scroller) + : 0; const rects = this.children.map(item => ({ height: item.height, top: getElementTop(item.$el) @@ -90,18 +88,21 @@ export default createComponent({ const active = this.getActiveAnchorIndex(scrollTop, rects); this.activeAnchorIndex = this.indexList[active]; - this.children.forEach((item, index) => { - if (index === active) { - item.active = true; - item.top = Math.max(this.stickyOffsetTop, rects[index].top - scrollTop); - } else if (index === active - 1) { - const activeItemTop = rects[active].top - scrollTop; - item.active = activeItemTop > 0; - item.top = activeItemTop - item.height; - } else { - item.active = false; - } - }); + + if (this.sticky) { + this.children.forEach((item, index) => { + if (index === active) { + item.active = true; + item.top = Math.max(this.stickyOffsetTop, rects[index].top - scrollTop); + } else if (index === active - 1) { + const activeItemTop = rects[active].top - scrollTop; + item.active = activeItemTop > 0; + item.top = activeItemTop - item.height; + } else { + item.active = false; + } + }); + } }, getActiveAnchorIndex(scrollTop, rects) { diff --git a/src/index-bar/test/__snapshots__/index.spec.js.snap b/src/index-bar/test/__snapshots__/index.spec.js.snap index a186b4f79..bef60e616 100644 --- a/src/index-bar/test/__snapshots__/index.spec.js.snap +++ b/src/index-bar/test/__snapshots__/index.spec.js.snap @@ -14,7 +14,7 @@ exports[`custom anchor text 1`] = ` exports[`scroll and update active anchor 1`] = `
-
ABCDEFGHIJKLMNOPQRSTUVWXYZ
+
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1