taro 返回屏幕可用高度

1.封装工具类

src/utils/style.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Taro from '@tarojs/taro'
 
const NAVIGATOR_HEIGHT = 44
const TAB_BAR_HEIGHT = 50
 
/**
 * 返回屏幕可用高度
 * // NOTE 各端返回的 windowHeight 不一定是最终可用高度(例如可能没减去 statusBar 的高度),需二次计算
 * @param {*} showTabBar
 */
export function getWindowHeight(showTabBar = true) {
  const info = Taro.getSystemInfoSync()
  const { windowHeight, statusBarHeight, titleBarHeight } = info
  const tabBarHeight = showTabBar ? TAB_BAR_HEIGHT : 0
 
  if (process.env.TARO_ENV === 'rn') {
    return windowHeight - statusBarHeight - NAVIGATOR_HEIGHT - tabBarHeight
  }
 
  if (process.env.TARO_ENV === 'h5') {
    return `${windowHeight - tabBarHeight}px`
  }
 
  if (process.env.TARO_ENV === 'alipay') {
    // NOTE 支付宝比较迷,windowHeight 似乎是去掉了 tabBar 高度,但无 tab 页跟 tab 页返回高度是一样的
    return `${windowHeight - statusBarHeight - titleBarHeight + (showTabBar ? 0 : TAB_BAR_HEIGHT)}px`
  }
 
  return `${windowHeight}px`
}

.

posted @   每天都要进步一点点  阅读(2903)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示