diff --git a/packages/uni-h5/src/service/api/ui/pageScrollTo.ts b/packages/uni-h5/src/service/api/ui/pageScrollTo.ts index a3404c671fb23e7a4d87ede610e9d03183aa84cc..1c1019f40fd3af758619d0c2986d4150d30e4a69 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 dd4307f1db720712c5ccdfca7156b6ed23629af7..6b6719105af2b90c4a039880a1db8a29bb45c287 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 3a58966cd815161da18ec8b2f42e730a90bc294b..93647808dbd7eb4dd33bf0a06ab0fe64698e5541 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 581b0e3aff3b14d4a0f4973e1f94712df79377c0..377d2630ef17abfc7cb46e33cacb6428e50b3bf4 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 6fb14f41c557e629befe00965b70c2fd4305cdfb..f7d274d1ae4b92b574c848bf267f81d11e953b38 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) {