system.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const SYSTEM_INFO = uni.getSystemInfoSync();
  2. /**
  3. * 状态栏高度
  4. * @returns {number}
  5. */
  6. export const getStatusBarHeight = () => {
  7. return SYSTEM_INFO.safeAreaInsets.top || 0;
  8. }
  9. /**
  10. * 获取胶囊按钮高度(即微信小程序上边的按钮)
  11. * @returns {number}
  12. */
  13. export const getCapsuleBarHeight = () => {
  14. if (uni.getMenuButtonBoundingClientRect) {
  15. let {
  16. top,
  17. height
  18. } = uni.getMenuButtonBoundingClientRect();
  19. return height + (top - getStatusBarHeight()) * 2;
  20. }
  21. return 40;
  22. }
  23. /**
  24. * 获取胶囊按钮信息(即微信小程序上边的按钮)
  25. * capsuleBarHeight 胶囊按钮栏高度
  26. * capsuleBarLeft 胶囊按钮栏左边距离
  27. * capsuleBarMargin 胶囊按钮栏边距
  28. * capsuleBarMarginTop 胶囊按钮栏上边距
  29. * capsuleBarMarginBottom 胶囊按钮栏下边距
  30. * capsuleBarContentHeight 胶囊按钮内容高度
  31. * @returns {{capsuleBarHeight: number, capsuleBarLeft: number, capsuleBarMarginTop: number, capsuleBarMarginBottom: number, capsuleBarContentHeight: number}}
  32. */
  33. export const getCapsuleBarInfo = () => {
  34. if (uni.getMenuButtonBoundingClientRect) {
  35. let { top, height, left} = uni.getMenuButtonBoundingClientRect();
  36. let barMargin = top - getStatusBarHeight()
  37. return {
  38. "capsuleBarHeight": height + barMargin * 2,
  39. "capsuleBarLeft": left,
  40. "capsuleBarMargin": barMargin,
  41. "capsuleBarMarginTop": barMargin,
  42. "capsuleBarMarginBottom": barMargin,
  43. "capsuleBarContentHeight": height
  44. }
  45. }
  46. return {
  47. "capsuleBarHeight": 40,
  48. "capsuleBarLeft": SYSTEM_INFO.screenWidth,
  49. "capsuleBarMargin": 4,
  50. "capsuleBarMarginTop": 4,
  51. "capsuleBarMarginBottom": 4,
  52. "capsuleBarContentHeight": 32
  53. };
  54. }
  55. /**
  56. * 获取安全区数据
  57. * statusBarHeight: 状态栏高度
  58. * capsuleBarHeight: 胶囊按钮高度
  59. * topHeight: 状态栏+胶囊按钮高度
  60. * bottomHeight: 底部安全区高度
  61. * capsuleBarHeight 胶囊按钮栏高度
  62. * capsuleBarLeft 胶囊按钮栏左边距离
  63. * capsuleBarMargin 胶囊按钮栏边距
  64. * capsuleBarMarginTop 胶囊按钮栏上边距
  65. * capsuleBarMarginBottom 胶囊按钮栏下边距
  66. * capsuleBarContentHeight 胶囊按钮内容高度
  67. * @returns {{topHeight: number, bottomHeight: number, statusBarHeight: number, capsuleBarHeight: number, capsuleBarLeft: number, capsuleBarMarginTop: number, capsuleBarMarginBottom: number, capsuleBarContentHeight: number}}
  68. */
  69. export const getSafeArea = () => {
  70. let tempSafeArea = getCapsuleBarInfo();
  71. tempSafeArea.statusBarHeight = getStatusBarHeight();
  72. tempSafeArea.bottomHeight = SYSTEM_INFO.safeAreaInsets.bottom || 0;
  73. tempSafeArea.topHeight = getStatusBarHeight() + tempSafeArea.capsuleBarHeight;
  74. return tempSafeArea;
  75. }