Skip to content

Commit

Permalink
feat(anchor): [anchor] add top-offset props
Browse files Browse the repository at this point in the history
  • Loading branch information
gimmyhehe committed Oct 24, 2024
1 parent 2d47f95 commit 2116397
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
18 changes: 9 additions & 9 deletions packages/renderless/src/anchor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ export const onItersectionObserver =
({ state, props, api, vm, emit }: Pick<IAnchorRenderlessParams, 'state' | 'props' | 'api' | 'vm' | 'emit'>) =>
() => {
const { expandLink, scrollContainer } = state
const { topOffset } = props
state.currentLink && updateSkidPosition({ vm, state, emit })
const rootMargin = topOffset ? `${-topOffset}px 0px 0px 0px` : ''
state.intersectionObserver = new IntersectionObserver(
(entries) => {
const { top } = scrollContainer.getBoundingClientRect()
const scrollStartTop = top + state.offsetTop
entries.forEach((item) => {
const key = item.target.id
state.observerLinks[key] = item
Expand All @@ -194,11 +194,7 @@ export const onItersectionObserver =
for (let key in state.observerLinks) {
if (Object.prototype.hasOwnProperty.call(state.observerLinks, key)) {
const item = state.observerLinks[key]
if (
item.isIntersecting &&
item.intersectionRatio >= 0 &&
item.target.getBoundingClientRect().top < scrollStartTop
) {
if (item.isIntersecting && item.intersectionRatio >= 0) {
const link = `#${item.target.id}`
if (!expandLink[link].children) {
api.getCurrentAnchor(link)
Expand All @@ -210,7 +206,7 @@ export const onItersectionObserver =
}
}
},
{ root: scrollContainer, threshold: [0, 0.25, 0.5, 1] }
{ root: scrollContainer, threshold: [0, 0.25, 0.5, 1], rootMargin }
)

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

if (scrollContainer && scrollContainer !== document.body && !isChangeHash) {
const linkEl = scrollContainer.querySelector(item.link) as HTMLElement
const top = linkEl?.offsetTop - scrollContainer.offsetTop // 修复横向锚点无法滚动到顶部
const top =
linkEl?.getBoundingClientRect().top -
scrollContainer.getBoundingClientRect().top +
scrollContainer.scrollTop -
props.topOffset // 修复横向锚点无法滚动到顶部
const param = { top, left: 0, behavior: 'smooth' } as ScrollToOptions
scrollContainer?.scrollTo(param)
scrollContainer?.addEventListener('scroll', api.handleScroll())
Expand Down
4 changes: 4 additions & 0 deletions packages/vue/src/anchor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const anchorProps = {
type: {
type: String,
default: 'line'
},
topOffset: {
type: Number,
default: 0
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/anchor/src/pc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { IAnchorApi } from '@opentiny/vue-renderless/types/anchor.type'
export default defineComponent({
name: $prefix + 'Anchor',
directives: { AutoTip },
props: [...props, 'isAffix', 'links', 'containerId', 'markClass', 'type'],
props: [...props, 'isAffix', 'links', 'containerId', 'markClass', 'type', 'topOffset'],
emits: ['linkClick', 'onChange', 'change'], // deprecated v3.12.0废弃,v3.17.0移除onChange 事件
setup(props, context) {
return setup({ props, context, renderless, api }) as unknown as IAnchorApi
Expand Down

0 comments on commit 2116397

Please sign in to comment.