You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

order-tabbar.vue 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. console.log("index", index)
  46. if (refresh) {
  47. uni.$emit("refreshOrder");
  48. return;
  49. }
  50. if (props.curIndex != index) {
  51. emit('update:curIndex', index)
  52. }
  53. }
  54. defineExpose({
  55. changeTab
  56. })
  57. </script>
  58. <style lang="scss">
  59. .tab-bar {
  60. width: 100%;
  61. background-color: white;
  62. padding-bottom: 8rpx;
  63. // border-bottom: 1px solid #efeff4;
  64. box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
  65. // z-index: 9990;
  66. // position: fixed;
  67. // left: 0;
  68. .bar-view {
  69. width: 100%;
  70. text-align: center;
  71. background-color: white;
  72. white-space: nowrap;
  73. /* +++ 不换行*/
  74. position: sticky;
  75. top: 0;
  76. .bar-item {
  77. flex: 1; //平均分配
  78. font-size: 28rpx;
  79. line-height: 80rpx;
  80. position: relative;
  81. display: inline-block;
  82. /*+++ 在一行显示*/
  83. min-width: 150rpx;
  84. &:after {
  85. position: absolute;
  86. content: '';
  87. width: 0;
  88. height: 0;
  89. border-bottom: 6rpx solid #00B38B;
  90. left: 50%;
  91. bottom: 18rpx;
  92. z-index: -99;
  93. height: 16rpx;
  94. background: #00b38b;
  95. opacity: 0.3;
  96. transform: translateX(-50%);
  97. transition: .3s;
  98. }
  99. }
  100. .active {
  101. // color: #00B38B;
  102. font-weight: bold;
  103. font-size: 30rpx;
  104. &:after {
  105. width: 70%;
  106. border-radius: 6rpx;
  107. }
  108. &:first-child {
  109. &:after {
  110. width: 50%;
  111. }
  112. }
  113. &:last-child {
  114. &:after {
  115. width: 95%;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. </style>