u-status-bar.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view
  3. :style="[style]"
  4. class="u-status-bar"
  5. :class="[isH5 && 'u-safe-area-inset-top']"
  6. >
  7. <slot />
  8. </view>
  9. </template>
  10. <script>
  11. import { props } from './props';
  12. import { mpMixin } from '../../libs/mixin/mpMixin';
  13. import { mixin } from '../../libs/mixin/mixin';
  14. import { addUnit, addStyle, deepMerge, getWindowInfo } from '../../libs/function/index';
  15. /**
  16. * StatbusBar 状态栏占位
  17. * @description 本组件主要用于状态填充,比如在自定导航栏的时候,它会自动适配一个恰当的状态栏高度。
  18. * @tutorial https://uview-plus.jiangruyi.com/components/statusBar.html
  19. * @property {String} bgColor 背景色 (默认 'transparent' )
  20. * @property {String | Object} customStyle 自定义样式
  21. * @example <u-status-bar></u-status-bar>
  22. */
  23. export default {
  24. name: 'u-status-bar',
  25. mixins: [mpMixin, mixin, props],
  26. data() {
  27. return {
  28. isH5: false
  29. }
  30. },
  31. created() {
  32. // #ifdef H5
  33. this.isH5 = true
  34. // #endif
  35. },
  36. emits: ['update:height'],
  37. computed: {
  38. style() {
  39. const style = {}
  40. // 状态栏高度,由于某些安卓和微信开发工具无法识别css的顶部状态栏变量,所以使用js获取的方式
  41. let sheight = getWindowInfo().statusBarHeight
  42. this.$emit('update:height', sheight)
  43. if (sheight == 0) {
  44. this.isH5 = true
  45. } else {
  46. style.height = addUnit(sheight, 'px')
  47. }
  48. style.backgroundColor = this.bgColor
  49. return deepMerge(style, addStyle(this.customStyle))
  50. }
  51. },
  52. }
  53. </script>
  54. <style lang="scss" scoped>
  55. .u-status-bar {
  56. // nvue会默认100%,如果nvue下,显式写100%的话,会导致宽度不为100%而异常
  57. /* #ifndef APP-NVUE */
  58. width: 100%;
  59. /* #endif */
  60. }
  61. </style>