Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

feat(anchor): 添加锚点anchor组件onchange事件 #13

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/anchor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ const setScrollContainer = ({ state, api, cb = null }) => {
}
}

const updateSkidPosition = ({ vm, state }) => {
const activeEl = document.querySelector(`a[href='${state.currentLink}']`)
const updateSkidPosition = ({ vm, state, emit }) => {
const { currentLink } = state
const activeEl = document.querySelector(`a[href='${currentLink}']`)
const { skidRef, maskRef, anchorRef } = vm.$refs

if (!activeEl || !anchorRef) {
return
}
emit('onChange', currentLink)

const { offsetHeight, offsetWidth } = activeEl
const { top: linkTitleClientTop, left: linkTitleClientLeft } = activeEl.getBoundingClientRect()
Expand All @@ -68,9 +70,10 @@ const updateSkidPosition = ({ vm, state }) => {
}
}

const getCurrentAnchor = ({ vm, state, link }) => {
const getCurrentAnchor = ({ vm, state, link, emit }) => {
if (state.currentLink === link) { return }
state.currentLink = link
updateSkidPosition({ vm, state })
updateSkidPosition({ vm, state, emit })
}

const addObserver = ({ props, state }) => {
Expand Down Expand Up @@ -119,7 +122,7 @@ export const unmounted = ({ state }) => () => {
intersectionObserver.disconnect()
}

export const onItersectionObserver = ({ vm, state, props }) => () => {
export const onItersectionObserver = ({ vm, state, props, emit }) => () => {

state.intersectionObserver = new IntersectionObserver((entries) => {
entries.forEach(item => {
Expand All @@ -130,7 +133,7 @@ export const onItersectionObserver = ({ vm, state, props }) => () => {
for (let item of Object.values(state.observerLinks)) {
if (item.isIntersecting && item.intersectionRatio > 0) {
const link = `#${item.target.id}`
getCurrentAnchor({ vm, state, link })
getCurrentAnchor({ vm, state, link, emit })
break
}
}
Expand All @@ -148,7 +151,7 @@ export const linkClick = ({ state, vm, emit, props }) => (e, item) => {
const isChangeHash = setCurrentHash(state)
const { scrollContainer } = state
state.currentLink = link
updateSkidPosition({ vm, state })
updateSkidPosition({ vm, state, emit })
setMarkClass({ state, props })

if (scrollContainer !== document.body && !isChangeHash) {
Expand Down
2 changes: 1 addition & 1 deletion src/anchor/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const renderless = (props, { onMounted, onUnmounted, onUpdated, reactive
unmounted: unmounted({ state }),
getContainer: getContainer({ props }),
linkClick: linkClick({ state, vm, emit, props }),
onItersectionObserver: onItersectionObserver({ vm, state, props })
onItersectionObserver: onItersectionObserver({ vm, state, props, emit })
})

onMounted(api.mounted)
Expand Down