capsule.vue 2.3 KB

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