From bc288a384856e4e4af4d608f488f3e96dc68ef0f Mon Sep 17 00:00:00 2001 From: zhangjing1 Date: Mon, 16 Mar 2026 11:04:35 +0800 Subject: [PATCH] bug(custom): --- src/pages/home/wgts/location-module/index.jsx | 3 +- src/pages/index.js | 45 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/pages/home/wgts/location-module/index.jsx b/src/pages/home/wgts/location-module/index.jsx index 7d1e5b172..7bbb86872 100644 --- a/src/pages/home/wgts/location-module/index.jsx +++ b/src/pages/home/wgts/location-module/index.jsx @@ -111,7 +111,8 @@ export default function WgtLocationModule(props) { setCurrentIndex(index) let viewIndex = index > 0 ? index - 1 : 0 setScrollView(`nav-item-${viewIndex}-${id}`) - setScrollIntoView(`#content-section-${index}-${id}`) + // scrollIntoView 传纯 id,不要 # 前缀,否则 H5/小程序 可能匹配不到导致滚回顶部 + setScrollIntoView(`content-section-${index}-${id}`) setTimeout(() => { isClickTab.current = false }, 500) diff --git a/src/pages/index.js b/src/pages/index.js index f40b4062f..711153a79 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -151,6 +151,45 @@ function Home() { } }, [skuPanelOpen]) + // H5:手动滚动到目标 section,等布局完成再测量并预留吸顶导航高度 + useEffect(() => { + if (process.env.TARO_ENV !== 'h5' || !scrollIntoView) return + const id = String(scrollIntoView).replace(/^#/, '') + if (!id) return + const navH = state.navbarHeight || 0 + let cancelled = false + const run = () => { + if (cancelled) return + Taro.createSelectorQuery() + .select('.home-body') + .node() + .exec((res) => { + if (cancelled) return + let scrollNode = res?.[0]?.node + if (!scrollNode) return + if (scrollNode.scrollHeight <= scrollNode.clientHeight && scrollNode.firstElementChild) { + const child = scrollNode.firstElementChild + if (child.scrollHeight > child.clientHeight) scrollNode = child + } + const target = document.getElementById(id) + if (!target) return + const targetRect = target.getBoundingClientRect() + const scrollRect = scrollNode.getBoundingClientRect() + const top = scrollNode.scrollTop + (targetRect.top - scrollRect.top) - navH + scrollNode.scrollTo({ top: Math.max(0, top), behavior: 'smooth' }) + }) + } + const t = setTimeout(() => { + requestAnimationFrame(() => { + requestAnimationFrame(run) + }) + }, 80) + return () => { + cancelled = true + clearTimeout(t) + } + }, [scrollIntoView, state.navbarHeight]) + useShareAppMessage(async () => { const { title, imageUrl } = await api.wx.shareSetting({ shareindex: 'index' }) let params = getCurrentPageRouteParams() @@ -311,11 +350,11 @@ function Home() { > { - pageRef.current.handlePageScroll(e?.detail) + pageRef.current?.handlePageScroll(e?.detail) }} - scrollIntoView={scrollIntoView} + scrollIntoView={process.env.TARO_ENV === 'h5' ? undefined : scrollIntoView} style={{ height: state.bodyHeight }} className={classNames('home-body', { 'has-home-header': isShowHomeHeader && isWeixin