From 3bbf0cb89a052603d7619a8ec36410360d15aa66 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 24 Jan 2019 18:16:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E7=9B=B8=E5=85=B3API=EF=BC=9Auni.onWindowResize=E3=80=81uni.of?= =?UTF-8?q?fWindowResize=20#133?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/platforms/h5/components/app/index.vue | 26 ++++++----- src/platforms/h5/service/api/window.js | 53 +++++++++++++++++++++++ 2 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 src/platforms/h5/service/api/window.js diff --git a/src/platforms/h5/components/app/index.vue b/src/platforms/h5/components/app/index.vue index 21b444ca23..1718eeedff 100644 --- a/src/platforms/h5/components/app/index.vue +++ b/src/platforms/h5/components/app/index.vue @@ -6,21 +6,21 @@ - - - - - @@ -78,6 +78,8 @@ export default { document.documentElement.style.setProperty('--window-bottom', windowBottom) console.debug(`uni.${windowBottom ? 'showTabBar' : 'hideTabBar'}:--window-bottom=${windowBottom}`) } + // 触发 resize 事件 + window.dispatchEvent(new CustomEvent('resize')) } }, created () { @@ -111,4 +113,4 @@ export default { width: 100%; height: 100%; } - + diff --git a/src/platforms/h5/service/api/window.js b/src/platforms/h5/service/api/window.js new file mode 100644 index 0000000000..665ca3e167 --- /dev/null +++ b/src/platforms/h5/service/api/window.js @@ -0,0 +1,53 @@ +const callbacks = [] + +function onResize () { + const { + invokeCallbackHandler: invoke + } = UniServiceJSBridge + callbacks.forEach(callbackId => { + const { + windowWidth, + windowHeight, + screenWidth, + screenHeight + } = uni.getSystemInfoSync() + var landscape = typeof window.orientation === 'number' ? Math.abs(window.orientation) === 90 : screenWidth / screenHeight > 1 + var deviceOrientation = landscape ? 'landscape' : 'portrait' + + invoke(callbackId, { + deviceOrientation, + size: { + windowWidth, + windowHeight, + screenWidth, + screenHeight + } + }) + }) +} +/** + * 监听窗口大小变化 + * @param {*} callbackId + */ +export function onWindowResize (callbackId) { + if (!callbacks.length) { + window.addEventListener('resize', onResize) + } + callbacks.push(callbackId) +} +/** + * 取消监听窗口大小变化 + * @param {*} callbackId + */ +export function offWindowResize (callbackId) { + var index = callbacks.indexOf(callbackId) + // 此处和微信平台一致查询不到去掉最后一个 + if (index >= 0) { + callbacks.splice(index, 1) + } else { + callbacks.pop() + } + if (!callbacks.length) { + window.removeEventListener('resize', onResize) + } +} -- GitLab