refactor(anchor): throttle scroll raf

This commit is contained in:
07akioni 2021-04-21 12:25:34 +08:00
parent 1fb128b821
commit 4c0678d23a

View File

@ -11,7 +11,7 @@ import {
onMounted,
toRef
} from 'vue'
import { getScrollParent, unwrapElement } from 'seemly'
import { getScrollParent, unwrapElement, beforeNextFrameOnce } from 'seemly'
import { onFontsReady } from 'vooks'
import { warn, keysOf } from '../../_utils'
import { anchorInjectionKey } from './Link'
@ -157,7 +157,7 @@ export default defineComponent({
}
}
function setActiveHref (href: string, transition = true): void {
const idMatchResult = /#([^#]+)$/.exec(href)
const idMatchResult = /^#([^#]+)$/.exec(href)
if (idMatchResult) {
const linkEl = document.getElementById(idMatchResult[1])
if (linkEl && scrollElement) {
@ -176,7 +176,7 @@ export default defineComponent({
}
}
function handleScroll (): void {
_handleScroll()
beforeNextFrameOnce(_handleScroll)
}
function _handleScroll (transition = true): void {
interface LinkInfo {
@ -254,12 +254,10 @@ export default defineComponent({
}
if (scrollEl) {
scrollElement = scrollEl
scrollElement.addEventListener('scroll', handleScroll, true)
} else if (__DEV__) {
warn('anchor', 'Target to be listened to is not valid.')
}
if (scrollEl) {
scrollEl.addEventListener('scroll', handleScroll)
}
}
provide(anchorInjectionKey, {
activeHref: activeHrefRef,