提交 ac82a6b2 编写于 作者: D DCloud_LXH

feat: onWindowResize、offWindowResize

上级 787956f8
......@@ -66,6 +66,7 @@ export * from './protocols/ui/showToast'
export * from './protocols/ui/startPullDownRefresh'
export * from './protocols/ui/stopPullDownRefresh'
export * from './protocols/ui/tabBar'
export * from './protocols/ui/window'
// helpers
export {
defineOnApi,
......
export const API_ON_WINDOW_RESIZE = 'onWindowResize'
export type API_TYPE_ON_WINDOW_RESIZE = typeof uni.onWindowResize
export const API_OFF_WINDOW_RESIZE = 'offWindowResize'
export type API_TYPE_OFF_WINDOW_RESIZE = typeof uni.offWindowResize
此差异已折叠。
......@@ -58,6 +58,7 @@ export * from './ui/pageScrollTo'
export * from './ui/startPullDownRefresh'
export * from './ui/stopPullDownRefresh'
export * from './ui/tabBar'
export * from './ui/window'
export {
upx2px,
......
import {
API_ON_WINDOW_RESIZE,
API_TYPE_ON_WINDOW_RESIZE,
API_OFF_WINDOW_RESIZE,
API_TYPE_OFF_WINDOW_RESIZE,
defineOnApi,
defineOffApi,
} from '@dcloudio/uni-api'
var tasks: number[] = []
function onResize() {
tasks.push(
setTimeout(() => {
tasks.forEach((task) => clearTimeout(task))
tasks.length = 0
const { windowWidth, windowHeight, screenWidth, screenHeight } =
uni.getSystemInfoSync()
var landscape = Math.abs(Number(window.orientation)) === 90
var deviceOrientation = landscape ? 'landscape' : 'portrait'
UniServiceJSBridge.invokeOnCallback<API_TYPE_ON_WINDOW_RESIZE>(
API_ON_WINDOW_RESIZE,
{
// @ts-ignore
deviceOrientation,
size: {
windowWidth,
windowHeight,
screenWidth,
screenHeight,
},
}
)
}, 20)
)
}
/**
* 监听窗口大小变化
* @param {*} callbackId
*/
export const onWindowResize = defineOnApi<API_TYPE_ON_WINDOW_RESIZE>(
API_ON_WINDOW_RESIZE,
() => {
window.addEventListener('resize', onResize)
}
)
/**
* 取消监听窗口大小变化
* @param {*} callbackId
*/
export const offWindowResize = defineOffApi<API_TYPE_OFF_WINDOW_RESIZE>(
API_OFF_WINDOW_RESIZE,
() => {
window.removeEventListener('resize', onResize)
}
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册