From 4c0678d23a0ae3a0283020995ecba96cfbde2f85 Mon Sep 17 00:00:00 2001 From: 07akioni <07akioni2@gmail.com> Date: Wed, 21 Apr 2021 12:25:34 +0800 Subject: [PATCH] refactor(anchor): throttle scroll raf --- src/anchor/src/BaseAnchor.tsx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/anchor/src/BaseAnchor.tsx b/src/anchor/src/BaseAnchor.tsx index 5cb17ddce..19ec67c4e 100644 --- a/src/anchor/src/BaseAnchor.tsx +++ b/src/anchor/src/BaseAnchor.tsx @@ -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,