From bd5c07ea72bb219329e9a32fdc76c759b63a0b59 Mon Sep 17 00:00:00 2001 From: Evan-k <48147837+k983551019@users.noreply.github.com> Date: Mon, 16 Dec 2024 21:17:59 +0800 Subject: [PATCH] fix(components): [anchor] scroll whether link is selected at the top (#18047) fix(components): [anchor] Scroll whether link is selected at the top Co-authored-by: sea <45450994+warmthsea@users.noreply.github.com> Co-authored-by: btea <2356281422@qq.com> --- packages/components/anchor/src/anchor.ts | 7 +++++++ packages/components/anchor/src/anchor.vue | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/components/anchor/src/anchor.ts b/packages/components/anchor/src/anchor.ts index eb49000867..db4c0acdec 100644 --- a/packages/components/anchor/src/anchor.ts +++ b/packages/components/anchor/src/anchor.ts @@ -60,6 +60,13 @@ export const anchorProps = buildProps({ type: definePropType<'vertical' | 'horizontal'>(String), default: 'vertical', }, + /** + * @description Scroll whether link is selected at the top + */ + scrollTop: { + type: Boolean, + default: false, + }, }) export type AnchorProps = ExtractPropTypes diff --git a/packages/components/anchor/src/anchor.vue b/packages/components/anchor/src/anchor.vue index bd6ac460ec..e1105fd2f8 100644 --- a/packages/components/anchor/src/anchor.vue +++ b/packages/components/anchor/src/anchor.vue @@ -134,13 +134,12 @@ const getCurrentHref = () => { }) } anchorTopList.sort((prev, next) => prev.top - next.top) - for (let i = 0; i < anchorTopList.length; i++) { const item = anchorTopList[i] const next = anchorTopList[i + 1] if (i === 0 && scrollTop === 0) { - return '' + return props.scrollTop ? item.href : '' } if (item.top <= scrollTop && (!next || next.top > scrollTop)) { return item.href