提交 3bbf0cb8 编写于 作者: Q qiang

feat: 实现窗口相关API:uni.onWindowResize、uni.offWindowResize #133

上级 3f4fa4cd
......@@ -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 () {
......
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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册