From bc78cc6d8c598457267071fc1661b4f5f869bce1 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Mon, 28 Mar 2022 15:48:06 +0800 Subject: [PATCH] fix(h5): pageScrollTo selector --- packages/uni-h5/src/service/api/ui/pageScrollTo.ts | 2 +- packages/uni-shared/dist/uni-shared.cjs.js | 8 ++++++-- packages/uni-shared/dist/uni-shared.d.ts | 2 +- packages/uni-shared/dist/uni-shared.es.js | 8 ++++++-- packages/uni-shared/src/dom/index.ts | 12 ++++++++++-- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/uni-h5/src/service/api/ui/pageScrollTo.ts b/packages/uni-h5/src/service/api/ui/pageScrollTo.ts index a3404c671..1c1019f40 100644 --- a/packages/uni-h5/src/service/api/ui/pageScrollTo.ts +++ b/packages/uni-h5/src/service/api/ui/pageScrollTo.ts @@ -10,7 +10,7 @@ import { scrollTo } from '@dcloudio/uni-shared' export const pageScrollTo = defineAsyncApi( API_PAGE_SCROLL_TO, ({ scrollTop, selector, duration }, { resolve }) => { - scrollTo(selector! || scrollTop! || 0, duration!) + scrollTo(selector! || scrollTop! || 0, duration!, true) resolve() }, PageScrollToProtocol, diff --git a/packages/uni-shared/dist/uni-shared.cjs.js b/packages/uni-shared/dist/uni-shared.cjs.js index dd4307f1d..6b6719105 100644 --- a/packages/uni-shared/dist/uni-shared.cjs.js +++ b/packages/uni-shared/dist/uni-shared.cjs.js @@ -504,11 +504,15 @@ function addFont(family, source, desc) { resolve(); }); } -function scrollTo(scrollTop, duration) { +function scrollTo(scrollTop, duration, isH5) { if (shared.isString(scrollTop)) { const el = document.querySelector(scrollTop); if (el) { - scrollTop = el.getBoundingClientRect().top + window.pageYOffset; + const { height, top } = el.getBoundingClientRect(); + scrollTop = top + window.pageYOffset; + if (isH5) { + scrollTop -= height; + } } } if (scrollTop < 0) { diff --git a/packages/uni-shared/dist/uni-shared.d.ts b/packages/uni-shared/dist/uni-shared.d.ts index 3a58966cd..93647808d 100644 --- a/packages/uni-shared/dist/uni-shared.d.ts +++ b/packages/uni-shared/dist/uni-shared.d.ts @@ -606,7 +606,7 @@ export declare const sanitise: (val: unknown) => any; export declare const SCHEME_RE: RegExp; -declare function scrollTo_2(scrollTop: number | string, duration: number): void; +declare function scrollTo_2(scrollTop: number | string, duration: number, isH5?: boolean): void; export { scrollTo_2 as scrollTo } export declare const SELECTED_COLOR = "#0062cc"; diff --git a/packages/uni-shared/dist/uni-shared.es.js b/packages/uni-shared/dist/uni-shared.es.js index 581b0e3af..377d2630e 100644 --- a/packages/uni-shared/dist/uni-shared.es.js +++ b/packages/uni-shared/dist/uni-shared.es.js @@ -500,11 +500,15 @@ function addFont(family, source, desc) { resolve(); }); } -function scrollTo(scrollTop, duration) { +function scrollTo(scrollTop, duration, isH5) { if (isString(scrollTop)) { const el = document.querySelector(scrollTop); if (el) { - scrollTop = el.getBoundingClientRect().top + window.pageYOffset; + const { height, top } = el.getBoundingClientRect(); + scrollTop = top + window.pageYOffset; + if (isH5) { + scrollTop -= height; + } } } if (scrollTop < 0) { diff --git a/packages/uni-shared/src/dom/index.ts b/packages/uni-shared/src/dom/index.ts index 6fb14f41c..f7d274d1a 100644 --- a/packages/uni-shared/src/dom/index.ts +++ b/packages/uni-shared/src/dom/index.ts @@ -56,11 +56,19 @@ export function addFont( }) } -export function scrollTo(scrollTop: number | string, duration: number) { +export function scrollTo( + scrollTop: number | string, + duration: number, + isH5?: boolean +) { if (isString(scrollTop)) { const el = document.querySelector(scrollTop) if (el) { - scrollTop = el.getBoundingClientRect().top + window.pageYOffset + const { height, top } = el.getBoundingClientRect() + scrollTop = top + window.pageYOffset + if (isH5) { + scrollTop -= height + } } } if (scrollTop < 0) { -- GitLab