capsule.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <view class="capsule" :style="{ backgroundColor: bgColor, paddingTop: top + 'px' }">
  3. <view class="title" v-if="showBorder" :style="{ height: height, paddingLeft: titleLeft, display: isSecurity ? 'none' : 'block' }">
  4. <slot name="top"></slot>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. created() {
  11. // #ifdef MP-WEIXIN
  12. // 获取胶囊按钮的位置
  13. let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  14. console.log(menuButtonInfo);
  15. this.height = menuButtonInfo.height + 'px'
  16. this.top = menuButtonInfo.top
  17. // 获取整个页面的宽度
  18. uni.getSystemInfo({
  19. success: res => {
  20. console.log(res);
  21. // 如果只需要安全区域的话 那就不需要高度
  22. if (this.isSecurity) {
  23. this.top = res.safeAreaInsets.bottom + 'px'
  24. return
  25. }
  26. this.titleLeft = res.windowWidth - menuButtonInfo.right + "px"
  27. }
  28. })
  29. // #endif
  30. },
  31. props: {
  32. title: {
  33. type: String,
  34. },
  35. bgColor: {
  36. type: String,
  37. },
  38. // 是不是只需要安全区域
  39. isSecurity: {
  40. type: Boolean,
  41. default: false
  42. },
  43. // 是否需要安全区域且于胶囊按钮平齐
  44. showBorder: {
  45. type: Boolean,
  46. default: false
  47. },
  48. showIcon: {
  49. type: Boolean,
  50. default: false
  51. },
  52. },
  53. data() {
  54. return {
  55. height: null,
  56. top: null,
  57. titleLeft: null,
  58. }
  59. },
  60. mounted () {
  61. // 计算该组件的高度并返回给父组件
  62. this.$emit('getHeight', this.top)
  63. },
  64. methods: {
  65. }
  66. }
  67. </script>
  68. <style lang="scss" scoped>
  69. .capsule {
  70. width: 100%;
  71. .title {
  72. padding-left: 30rpx;
  73. box-sizing: border-box;
  74. color: rgba(0, 0, 0, 0.8);
  75. font-size: 32rpx;
  76. font-weight: 700;
  77. display: flex !important;
  78. align-items: center;
  79. gap: 10rpx;
  80. }
  81. }
  82. </style>