u-status-bar.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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: true
  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. style.height = addUnit(getWindowInfo().statusBarHeight, 'px')
  41. style.backgroundColor = this.bgColor
  42. return deepMerge(style, addStyle(this.customStyle))
  43. }
  44. },
  45. }
  46. </script>
  47. <style lang="scss" scoped>
  48. .u-status-bar {
  49. // nvue会默认100%,如果nvue下,显式写100%的话,会导致宽度不为100%而异常
  50. /* #ifndef APP-NVUE */
  51. width: 100%;
  52. /* #endif */
  53. }
  54. </style>