diff --git a/packages/uni-core/src/helpers/dom.ts b/packages/uni-core/src/helpers/dom.ts index 0e1b9be588c1d6a874714234dca64061b6b24998..272e8ec57e0e4ad1885eaac5d4973f4280108d17 100644 --- a/packages/uni-core/src/helpers/dom.ts +++ b/packages/uni-core/src/helpers/dom.ts @@ -4,18 +4,22 @@ import safeAreaInsets from 'safe-area-insets' export const onEventPrevent = /*#__PURE__*/ withModifiers(() => {}, ['prevent']) export const onEventStop = /*#__PURE__*/ withModifiers(() => {}, ['stop']) +function getWindowOffsetCssVar(style: CSSStyleDeclaration, name: string) { + return parseInt((style.getPropertyValue(name).match(/\d+/) || ['0'])[0]) +} + export function getWindowTop() { const style = document.documentElement.style - const top = parseInt(style.getPropertyValue('--window-top')) + const top = getWindowOffsetCssVar(style, '--window-top') return top ? top + safeAreaInsets.top : 0 } export function getWindowOffset() { const style = document.documentElement.style const top = getWindowTop() - const bottom = parseInt(style.getPropertyValue('--window-bottom')) - const left = parseInt(style.getPropertyValue('--window-left')) - const right = parseInt(style.getPropertyValue('--window-right')) + const bottom = getWindowOffsetCssVar(style, '--window-bottom') + const left = getWindowOffsetCssVar(style, '--window-left') + const right = getWindowOffsetCssVar(style, '--window-right') return { top, bottom: bottom ? bottom + safeAreaInsets.bottom : 0, diff --git a/packages/uni-core/src/view/plugin/componentInstance.ts b/packages/uni-core/src/view/plugin/componentInstance.ts index 27b3e01e6a8d0191b8501b16334260bc723ad03d..edf092eff93d487db6edb7cea63758c39a144b35 100644 --- a/packages/uni-core/src/view/plugin/componentInstance.ts +++ b/packages/uni-core/src/view/plugin/componentInstance.ts @@ -92,7 +92,7 @@ function normalizeClickEvent( const { x, y } = mouseEvt const top = getWindowTop() evt.detail = { x, y: y - top } - evt.touches = evt.changedTouches = [createTouchEvent(mouseEvt)] + evt.touches = evt.changedTouches = [createTouchEvent(mouseEvt, top)] } function normalizeMouseEvent(evt: Record, mouseEvt: MouseEvent) { @@ -103,14 +103,14 @@ function normalizeMouseEvent(evt: Record, mouseEvt: MouseEvent) { evt.clientY = mouseEvt.clientY - top } -function createTouchEvent(evt: MouseEvent) { +function createTouchEvent(evt: MouseEvent, top: number) { return { force: 1, identifier: 0, clientX: evt.clientX, - clientY: evt.clientY, + clientY: evt.clientY - top, pageX: evt.pageX, - pageY: evt.pageY, + pageY: evt.pageY - top, } } diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 8b4c7278afd9b401f4f0a442256378a657d96f68..b8b0f3a88af56543a8b66e1b06a157e660d96007 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -737,17 +737,20 @@ const onEventPrevent = /* @__PURE__ */ withModifiers(() => { }, ["prevent"]); const onEventStop = /* @__PURE__ */ withModifiers(() => { }, ["stop"]); +function getWindowOffsetCssVar(style, name) { + return parseInt((style.getPropertyValue(name).match(/\d+/) || ["0"])[0]); +} function getWindowTop() { const style = document.documentElement.style; - const top = parseInt(style.getPropertyValue("--window-top")); + const top = getWindowOffsetCssVar(style, "--window-top"); return top ? top + out.top : 0; } function getWindowOffset() { const style = document.documentElement.style; const top = getWindowTop(); - const bottom = parseInt(style.getPropertyValue("--window-bottom")); - const left = parseInt(style.getPropertyValue("--window-left")); - const right = parseInt(style.getPropertyValue("--window-right")); + const bottom = getWindowOffsetCssVar(style, "--window-bottom"); + const left = getWindowOffsetCssVar(style, "--window-left"); + const right = getWindowOffsetCssVar(style, "--window-right"); return { top, bottom: bottom ? bottom + out.bottom : 0, @@ -1312,7 +1315,7 @@ function normalizeClickEvent(evt, mouseEvt) { const { x, y } = mouseEvt; const top = getWindowTop(); evt.detail = { x, y: y - top }; - evt.touches = evt.changedTouches = [createTouchEvent(mouseEvt)]; + evt.touches = evt.changedTouches = [createTouchEvent(mouseEvt, top)]; } function normalizeMouseEvent(evt, mouseEvt) { const top = getWindowTop(); @@ -1321,14 +1324,14 @@ function normalizeMouseEvent(evt, mouseEvt) { evt.clientX = mouseEvt.clientX; evt.clientY = mouseEvt.clientY - top; } -function createTouchEvent(evt) { +function createTouchEvent(evt, top) { return { force: 1, identifier: 0, clientX: evt.clientX, - clientY: evt.clientY, + clientY: evt.clientY - top, pageX: evt.pageX, - pageY: evt.pageY + pageY: evt.pageY - top }; } function normalizeTouchEvent(touches, top) {