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>
This commit is contained in:
Evan-k 2024-12-16 21:17:59 +08:00 committed by GitHub
parent 8b829ea7d3
commit bd5c07ea72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -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<typeof anchorProps>

View File

@ -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