diff --git a/packages/vant/src/index-bar/IndexBar.tsx b/packages/vant/src/index-bar/IndexBar.tsx index 6441281d3..ab136180f 100644 --- a/packages/vant/src/index-bar/IndexBar.tsx +++ b/packages/vant/src/index-bar/IndexBar.tsx @@ -140,7 +140,11 @@ export default defineComponent({ const match = getMatchAnchor(selectActiveIndex); if (match) { const rect = match.getRect(scrollParent.value, scrollParentRect); - active = getActiveAnchor(rect.top, rects); + if (props.sticky && props.stickyOffsetTop) { + active = getActiveAnchor(rect.top - props.stickyOffsetTop, rects); + } else { + active = getActiveAnchor(rect.top, rects); + } } } else { active = getActiveAnchor(scrollTop, rects); @@ -229,7 +233,11 @@ export default defineComponent({ } if (props.sticky && props.stickyOffsetTop) { - setRootScrollTop(getRootScrollTop() - props.stickyOffsetTop); + if (getRootScrollTop() === offsetHeight - scrollParentRect.height) { + setRootScrollTop(getRootScrollTop()); + } else { + setRootScrollTop(getRootScrollTop() - props.stickyOffsetTop); + } } emit('select', match.index); diff --git a/packages/vant/src/index-bar/test/__snapshots__/index.spec.jsx.snap b/packages/vant/src/index-bar/test/__snapshots__/index.spec.jsx.snap index 7635b1f60..c90509c59 100644 --- a/packages/vant/src/index-bar/test/__snapshots__/index.spec.jsx.snap +++ b/packages/vant/src/index-bar/test/__snapshots__/index.spec.jsx.snap @@ -6,6 +6,389 @@ exports[`should allow to custom anchor content 1`] = ` `; +exports[`should render active anchor when stick prop is true and has stickyOffsetTop 1`] = ` +
+`; + +exports[`should render active anchor when stick prop is true and has stickyOffsetTop 2`] = ` + +`; + exports[`should update active anchor after page scroll 1`] = `