diff --git a/src/index-bar/index.js b/src/index-bar/index.js index 2ad3b8f71..a1136b45b 100644 --- a/src/index-bar/index.js +++ b/src/index-bar/index.js @@ -4,6 +4,7 @@ import { ParentMixin } from '../mixins/relation'; import { BindEventMixin } from '../mixins/bind-event'; import { GREEN } from '../utils/constant'; import { preventDefault } from '../utils/dom/event'; +import { isHidden } from '../utils/dom/style'; import { getScrollTop, getElementTop, @@ -85,6 +86,10 @@ export default createComponent({ methods: { onScroll() { + if (isHidden(this.$el)) { + return; + } + const scrollTop = getScrollTop(this.scroller); const scrollerRect = this.getScrollerRect(); const rects = this.children.map(item => ({ @@ -101,7 +106,7 @@ export default createComponent({ let isReachEdge = false; if (active !== -1) { - activeItemTop = rects[active].top - scrollTop; + activeItemTop = rects[active].top - scrollTop - this.stickyOffsetTop; isReachEdge = activeItemTop <= 0; } @@ -153,8 +158,9 @@ export default createComponent({ getActiveAnchorIndex(scrollTop, rects) { for (let i = this.children.length - 1; i >= 0; i--) { const prevHeight = i > 0 ? rects[i - 1].height : 0; + const reachTop = this.sticky ? prevHeight + this.stickyOffsetTop : 0; - if (scrollTop + prevHeight + this.stickyOffsetTop >= rects[i].top) { + if (scrollTop + reachTop >= rects[i].top) { return i; } } @@ -195,7 +201,7 @@ export default createComponent({ if (match[0]) { match[0].scrollIntoView(); - if (this.stickyOffsetTop) { + if (this.sticky && this.stickyOffsetTop) { setRootScrollTop(getRootScrollTop() - this.stickyOffsetTop); } diff --git a/src/index-bar/test/__snapshots__/index.spec.js.snap b/src/index-bar/test/__snapshots__/index.spec.js.snap index a7f71a681..f18d2c546 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
@@ -32,7 +32,7 @@ exports[`scroll and update active anchor 1`] = ` exports[`scroll and update active anchor 2`] = `
-
ABCDEFGHIJKLMNOPQRSTUVWXYZ
+
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1