Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

order-tabbar.vue 2.8KB

2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
7 meses atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
7 meses atrás
2 anos atrás
7 meses atrás
2 anos atrás
7 meses atrás
2 anos atrás
1 ano atrás
2 anos atrás
1 ano atrás
2 anos atrás
7 meses atrás
2 anos atrás
1 ano atrás
2 anos atrás
7 meses atrás
2 anos atrás
1 ano atrás
2 anos atrás
7 meses atrás
2 anos atrás
1 ano atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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', 'index'])
  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. console.log("props.curIndex====",val)
  36. curIndex.value = val
  37. });
  38. /*监听标签数组变化,来计算每个标签占的宽度*/
  39. watch(() => props.tabs, (newVal) => {
  40. if (newVal && newVal.length < 5) {
  41. data.itemWidth = data.windowWidth / newVal.length
  42. }
  43. }, { immediate: true });
  44. /* 改变tab refresh:点击同个tab是否刷新*/
  45. const changeTab = (index, refresh ?: boolean) => {
  46. if (refresh) {
  47. console.log("index111", index)
  48. uni.$emit("refreshOrder");
  49. return;
  50. }
  51. if (props.curIndex != index) {
  52. emit('update:curIndex', index)
  53. console.log("index222", index)
  54. emit('index', index)
  55. }
  56. }
  57. defineExpose({
  58. changeTab
  59. })
  60. </script>
  61. <style lang="scss">
  62. .tab-bar {
  63. width: 100%;
  64. background-color: white;
  65. padding-bottom: 8rpx;
  66. box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
  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 #01243A;
  87. left: 50%;
  88. bottom: 8rpx;
  89. z-index: -99;
  90. height: 6rpx;
  91. background: #01243A;
  92. opacity: 0.3;
  93. transform: translateX(-50%);
  94. transition: .3s;
  95. }
  96. }
  97. .active {
  98. font-weight: bold;
  99. font-size: 30rpx;
  100. &:after {
  101. width: 50%;
  102. border-radius: 6rpx;
  103. }
  104. &:first-child {
  105. &:after {
  106. width: 30%;
  107. }
  108. }
  109. &:last-child {
  110. &:after {
  111. width: 75%;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. </style>