Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

order-tabbar.vue 2.8KB

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