Skip to content

Commit f5b8214

Browse files
authored
feat(anchor): [anchor] add top-offset props (#2388)
1 parent 61b013f commit f5b8214

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

packages/renderless/src/anchor/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ export const onItersectionObserver =
168168
({ state, props, api, vm, emit }: Pick<IAnchorRenderlessParams, 'state' | 'props' | 'api' | 'vm' | 'emit'>) =>
169169
() => {
170170
const { expandLink, scrollContainer } = state
171+
const { topOffset } = props
171172
state.currentLink && updateSkidPosition({ vm, state, emit })
173+
const rootMargin = topOffset ? `${-topOffset}px 0px 0px 0px` : ''
172174
state.intersectionObserver = new IntersectionObserver(
173175
(entries) => {
174-
const { top } = scrollContainer.getBoundingClientRect()
175-
const scrollStartTop = top + state.offsetTop
176176
entries.forEach((item) => {
177177
const key = item.target.id
178178
state.observerLinks[key] = item
@@ -194,11 +194,7 @@ export const onItersectionObserver =
194194
for (let key in state.observerLinks) {
195195
if (Object.prototype.hasOwnProperty.call(state.observerLinks, key)) {
196196
const item = state.observerLinks[key]
197-
if (
198-
item.isIntersecting &&
199-
item.intersectionRatio >= 0 &&
200-
item.target.getBoundingClientRect().top < scrollStartTop
201-
) {
197+
if (item.isIntersecting && item.intersectionRatio >= 0) {
202198
const link = `#${item.target.id}`
203199
if (!expandLink[link].children) {
204200
api.getCurrentAnchor(link)
@@ -210,7 +206,7 @@ export const onItersectionObserver =
210206
}
211207
}
212208
},
213-
{ root: scrollContainer, threshold: [0, 0.25, 0.5, 1] }
209+
{ root: scrollContainer, threshold: [0, 0.25, 0.5, 1], rootMargin }
214210
)
215211

216212
addObserver({ props, state })
@@ -232,7 +228,11 @@ export const linkClick =
232228

233229
if (scrollContainer && scrollContainer !== document.body && !isChangeHash) {
234230
const linkEl = scrollContainer.querySelector(item.link) as HTMLElement
235-
const top = linkEl?.offsetTop - scrollContainer.offsetTop // 修复横向锚点无法滚动到顶部
231+
const top =
232+
linkEl?.getBoundingClientRect().top -
233+
scrollContainer.getBoundingClientRect().top +
234+
scrollContainer.scrollTop -
235+
props.topOffset // 修复横向锚点无法滚动到顶部
236236
const param = { top, left: 0, behavior: 'smooth' } as ScrollToOptions
237237
scrollContainer?.scrollTo(param)
238238
scrollContainer?.addEventListener('scroll', api.handleScroll())

packages/vue/src/anchor/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export const anchorProps = {
3232
type: {
3333
type: String,
3434
default: 'line'
35+
},
36+
topOffset: {
37+
type: Number,
38+
default: 0
3539
}
3640
}
3741

packages/vue/src/anchor/src/pc.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { IAnchorApi } from '@opentiny/vue-renderless/types/anchor.type'
77
export default defineComponent({
88
name: $prefix + 'Anchor',
99
directives: { AutoTip },
10-
props: [...props, 'isAffix', 'links', 'containerId', 'markClass', 'type'],
10+
props: [...props, 'isAffix', 'links', 'containerId', 'markClass', 'type', 'topOffset'],
1111
emits: ['linkClick', 'onChange', 'change'], // deprecated v3.12.0废弃,v3.17.0移除onChange 事件
1212
setup(props, context) {
1313
return setup({ props, context, renderless, api }) as unknown as IAnchorApi

0 commit comments

Comments
 (0)