From 8b1b0626fe40abc183faad9deae74fa091a397a0 Mon Sep 17 00:00:00 2001 From: AC-0308 Date: Wed, 10 Dec 2025 10:03:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(utils):=20=E2=9C=A8=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=85=83=E7=B4=A0=E5=8F=AF=E8=A7=81=E6=80=A7=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=9A=84=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/opendesign/src/_utils/dom.ts | 51 +++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/opendesign/src/_utils/dom.ts b/packages/opendesign/src/_utils/dom.ts index 3fb153fac..21ed193dc 100644 --- a/packages/opendesign/src/_utils/dom.ts +++ b/packages/opendesign/src/_utils/dom.ts @@ -181,8 +181,55 @@ export function isOverflown(element?: HTMLElement) { if (!element) { return false; } + return element.scrollWidth > element.clientWidth || element.scrollHeight > element.clientHeight; +} + +/** + * 判断元素是否在视口内 + */ +export function isInViewport(element?: Element) { + if (!element) { + return false; + } + const rect = element.getBoundingClientRect(); return ( - element.scrollWidth > element.clientWidth || - element.scrollHeight > element.clientHeight + rect.top < (window.innerHeight || document.documentElement.clientHeight) && + rect.bottom > 0 && + rect.left < (window.innerWidth || document.documentElement.clientWidth) && + rect.right > 0 ); } + +/** + * 判断是否为不可见标签 + */ +export function isNonVisibleTag(element: Element) { + const nonVisibleTags = ['SCRIPT', 'STYLE', 'LINK', 'META', 'HEAD']; + return nonVisibleTags.includes(element.tagName); +} + +/** + * 判断元素是否在视觉上隐藏 + */ +export function isElementHidden(element: HTMLElement) { + // 直接检查内联样式 + if (element.style.display === 'none' || element.style.visibility === 'hidden') { + return true; + } + + // 通过getComputedStyle检查计算样式 + const computedStyle = window.getComputedStyle(element); + if ( + computedStyle.display === 'none' || + computedStyle.visibility === 'hidden' || + computedStyle.opacity === '0' || + computedStyle.width === '0px' || + computedStyle.height === '0px' + ) { + return true; + } + + // 检查元素是否在视口外或尺寸为0 + const rect = element.getBoundingClientRect(); + return rect.width === 0 || rect.height === 0; +} -- Gitee From 7873c72d95d3314689355da64c0710e59c8fe5ce Mon Sep 17 00:00:00 2001 From: AC-0308 Date: Wed, 10 Dec 2025 10:04:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(OAnchor):=20=F0=9F=90=9B=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=9C=A8=E8=A7=86=E5=8F=A3=E5=86=85=E4=BD=86anchor=E6=9C=AA?= =?UTF-8?q?=E8=A2=AB=E9=80=89=E4=B8=AD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/opendesign/src/anchor/OAnchor.vue | 19 ++++++++++++------- .../src/anchor/__demo__/AnchorBasic.vue | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/opendesign/src/anchor/OAnchor.vue b/packages/opendesign/src/anchor/OAnchor.vue index 6f385ffb4..76aa44abc 100644 --- a/packages/opendesign/src/anchor/OAnchor.vue +++ b/packages/opendesign/src/anchor/OAnchor.vue @@ -113,12 +113,7 @@ const scrollIntoView = async (link: string) => { isScrolling.value = false; }; -// 滚动事件 -const onScroll = () => { - if (isScrolling.value) { - return; - } - +const activeNearest = () => { const distances: Array<{ link: string; top: number }> = []; const { targetOffset, bounds } = props; @@ -144,6 +139,14 @@ const onScroll = () => { } setActiveLink(active); +} + +// 滚动事件 +const onScroll = () => { + if (isScrolling.value) { + return; + } + activeNearest(); }; const bindEvent = () => { @@ -151,7 +154,7 @@ const bindEvent = () => { return; } - scrollContainer.value.addEventListener('scroll', onScroll); + scrollContainer.value.addEventListener('scroll', onScroll, { passive: true }); }; const unbindEvent = () => { @@ -197,6 +200,8 @@ onMounted(() => { const hash = decodeURIComponent(window.location.hash); if (hash) { scrollIntoView(hash); + } else { + activeNearest(); } nextTick(() => { bindEvent(); diff --git a/packages/opendesign/src/anchor/__demo__/AnchorBasic.vue b/packages/opendesign/src/anchor/__demo__/AnchorBasic.vue index c08f01d9c..0068a74c4 100644 --- a/packages/opendesign/src/anchor/__demo__/AnchorBasic.vue +++ b/packages/opendesign/src/anchor/__demo__/AnchorBasic.vue @@ -16,7 +16,7 @@ import { OAnchor, OAnchorItem } from '../index';
medium:
- + -- Gitee