index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view class="tab" :style="{
  3. '--barheight': barHeight + 'px',
  4. '--color': color,
  5. '--bgcolor': bg,
  6. height: fixedHeight
  7. }">
  8. <view class="heads">
  9. <image class="bgImg" v-if="bgImg" :src="bgImg" mode=""></image>
  10. <slot name="left">
  11. <uni-icons v-if="show_back" type="left" size="30" :color="color" @click="back"></uni-icons>
  12. </slot>
  13. <slot name="title">
  14. <text class="title" :style="{'--size': titlesize}">{{ title }}</text>
  15. </slot>
  16. <slot name="right">
  17. <!-- <uni-icons type="left" size="30" color="transparent"></uni-icons> -->
  18. </slot>
  19. </view>
  20. <slot name="bottom"></slot>
  21. </view>
  22. </template>
  23. <script setup>
  24. import {
  25. ref,
  26. computed,
  27. onMounted,
  28. defineProps,
  29. defineEmits
  30. } from 'vue';
  31. const props = defineProps({
  32. title: { //标题
  33. type: String,
  34. default: ""
  35. },
  36. show_back: { //显示返回按钮
  37. type: Boolean,
  38. default: true
  39. },
  40. back_type: { //是否使用组件的返回
  41. type: Boolean,
  42. default: false
  43. },
  44. bg: { //背景
  45. type: String,
  46. default: '#fff'
  47. },
  48. color: { //标题的字体颜色
  49. type: String,
  50. default: 'white'
  51. },
  52. //背景图片
  53. bgImg: {
  54. default: ""
  55. },
  56. // 文字大小
  57. titlesize: {
  58. type: String,
  59. default: "32rpx"
  60. },
  61. bottomHeight: {
  62. type: String,
  63. default: "80rpx"
  64. },
  65. fixed: {
  66. type: Boolean,
  67. default: true
  68. }
  69. });
  70. const emit = defineEmits(['right']);
  71. // 状态栏高度
  72. const barHeight = ref(0);
  73. const rpxToPx = (rpx) => {
  74. const systemInfo = uni.getSystemInfoSync();
  75. return (rpx / 750) * systemInfo.windowWidth;
  76. };
  77. const fixedHeight = computed(() => {
  78. if (!props.fixed) return '0';
  79. // 将80rpx转换为px
  80. const navHeightPx = rpxToPx(80);
  81. return `${barHeight.value + navHeightPx}px`;
  82. });
  83. onMounted(() => {
  84. // 获取状态栏高度
  85. const systemInfo = uni.getSystemInfoSync();
  86. barHeight.value = systemInfo.statusBarHeight || 0;
  87. });
  88. // 返回按钮图片
  89. const back_image = computed(() => {
  90. return props.color == 'white' ? require('./down.png') : require('./back.png');
  91. });
  92. const back = () => {
  93. if (!props.show_back) return;
  94. uni.navigateBack({
  95. fail: () => {
  96. uni.reLaunch({
  97. url: uni.getStorageSync('userType') == 4 ?
  98. '/pages/index/dispose/index' : '/pages/index/index'
  99. });
  100. }
  101. });
  102. };
  103. const right = () => {
  104. emit('right');
  105. };
  106. </script>
  107. <style lang="scss" scoped>
  108. .tab {
  109. position: sticky;
  110. top: 0;
  111. left: 0;
  112. width: 100%;
  113. z-index: 999;
  114. }
  115. .heads {
  116. z-index: 100;
  117. width: 100%;
  118. height: calc(var(--barheight) + 80rpx);
  119. justify-content: space-between;
  120. display: flex;
  121. align-items: center;
  122. padding-top: var(--barheight);
  123. padding-left: 20rpx;
  124. padding-right: 20rpx;
  125. padding-bottom: 10rpx;
  126. background: var(--bgcolor);
  127. box-sizing: border-box;
  128. .bgImg {
  129. width: 100%;
  130. height: 100%;
  131. position: absolute;
  132. top: 0;
  133. left: 0;
  134. z-index: -1;
  135. }
  136. .title {
  137. width: 100%;
  138. text-align: center;
  139. font-size: var(--size);
  140. font-family: PingFang SC;
  141. font-weight: bold;
  142. color: var(--color);
  143. position: absolute;
  144. left: 0;
  145. pointer-events: none;
  146. }
  147. }
  148. </style>