u-status-bar.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. computed: {
  37. style() {
  38. const style = {}
  39. // 状态栏高度,由于某些安卓和微信开发工具无法识别css的顶部状态栏变量,所以使用js获取的方式
  40. let sheight = getWindowInfo().statusBarHeight
  41. if (sheight == 0) {
  42. this.isH5 = true
  43. } else {
  44. style.height = addUnit(sheight, 'px')
  45. }
  46. style.backgroundColor = this.bgColor
  47. return deepMerge(style, addStyle(this.customStyle))
  48. }
  49. },
  50. }
  51. </script>
  52. <style lang="scss" scoped>
  53. .u-status-bar {
  54. // nvue会默认100%,如果nvue下,显式写100%的话,会导致宽度不为100%而异常
  55. /* #ifndef APP-NVUE */
  56. width: 100%;
  57. /* #endif */
  58. }
  59. </style>