Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!-- 切换的tabBar -->
  2. <template>
  3. <view class="tab-bar" @touchmove.stop.prevent="()=>{}">
  4. <scroll-view id="nav-bar" class="noScorll bar-view" scroll-x :scroll-left="data.scrollLeft">
  5. <view v-for="(item,index) in tabs" :key="item.id" :class="{active:index === curIndex}" class="bar-item"
  6. :style="{width: `${data.itemWidth}px`}" @click="changeTab(index)">
  7. {{item.name}}
  8. </view>
  9. </scroll-view>
  10. </view>
  11. </template>
  12. <script setup lang="ts">
  13. import {ref,watch, reactive, PropType} from 'vue'
  14. const emit = defineEmits(['update:curIndex'])
  15. const props = defineProps({
  16. //当前选中的下标
  17. curIndex: {
  18. type: Number,
  19. default: 0
  20. },
  21. //标签选项数组
  22. tabs: {
  23. type: Array as PropType<{ id: number; name: string }[]>,
  24. default: () => []
  25. }
  26. })
  27. const data = reactive({
  28. scrollLeft: 0, //顶部选项卡左滑距离
  29. windowWidth: uni.getSystemInfoSync().windowWidth, //屏幕宽度
  30. itemWidth: uni.upx2px(150), //每个标签的宽度 默认150rpx
  31. })
  32. /* 查看父组件传过来的值是否变化,从而修改值 */
  33. const curIndex = ref(props.curIndex)
  34. watch(() => props.curIndex, (val) => {
  35. curIndex.value = val
  36. });
  37. /*监听标签数组变化,来计算每个标签占的宽度*/
  38. watch(() => props.tabs, (newVal) => {
  39. if (newVal && newVal.length < 5) {
  40. data.itemWidth = data.windowWidth / newVal.length
  41. }
  42. }, {immediate: true});
  43. /* 改变tab refresh:点击同个tab是否刷新*/
  44. const changeTab = (index,refresh?:boolean) => {
  45. if(refresh){
  46. uni.$emit("refreshOrder");
  47. return;
  48. }
  49. if (props.curIndex != index) {
  50. emit('update:curIndex', index)
  51. }
  52. }
  53. defineExpose({
  54. changeTab
  55. })
  56. </script>
  57. <style lang="scss">
  58. .tab-bar {
  59. width: 100%;
  60. background-color: white;
  61. padding-bottom: 8rpx;
  62. // border-bottom: 1px solid #efeff4;
  63. box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
  64. z-index: 9990;
  65. position: fixed;
  66. left: 0;
  67. .bar-view {
  68. width: 100%;
  69. text-align: center;
  70. background-color: white;
  71. white-space: nowrap; /* +++ 不换行*/
  72. position: sticky;
  73. top:0;
  74. .bar-item {
  75. flex: 1; //平均分配
  76. font-size: 28rpx;
  77. line-height: 80rpx;
  78. position: relative;
  79. display: inline-block; /*+++ 在一行显示*/
  80. min-width: 150rpx;
  81. &:after {
  82. position: absolute;
  83. content: '';
  84. width: 0;
  85. height: 0;
  86. border-bottom: 6rpx solid #00B38B;
  87. left: 50%;
  88. bottom: 18rpx;
  89. z-index: -99;
  90. height: 16rpx;
  91. background: #00b38b;
  92. opacity: 0.3;
  93. transform: translateX(-50%);
  94. transition: .3s;
  95. }
  96. }
  97. .active {
  98. // color: #00B38B;
  99. font-weight: bold;
  100. font-size:30rpx;
  101. &:after {
  102. width: 70%;
  103. border-radius: 6rpx;
  104. }
  105. &:first-child{
  106. &:after {
  107. width:50%;
  108. }
  109. }
  110. &:last-child{
  111. &:after {
  112. width: 95%;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. </style>