From 878a3868207786184d1cf5937a040310401e828a Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 20 Jan 2022 15:48:27 +0800 Subject: [PATCH] fix(h5): pageY and clientY --- packages/uni-core/src/helpers/dom.ts | 12 ++++++++---- .../src/view/plugin/componentInstance.ts | 8 ++++---- packages/uni-h5/dist/uni-h5.es.js | 19 +++++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/uni-core/src/helpers/dom.ts b/packages/uni-core/src/helpers/dom.ts index 0e1b9be58..272e8ec57 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 27b3e01e6..edf092eff 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 8b4c7278a..b8b0f3a88 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) { -- GitLab