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