選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

order-tabbar.vue 2.8KB

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'])
  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. position: sticky;
  74. top:0;
  75. .bar-item {
  76. flex: 1; //平均分配
  77. font-size: 28rpx;
  78. line-height: 80rpx;
  79. position: relative;
  80. display: inline-block; /*+++ 在一行显示*/
  81. min-width: 150rpx;
  82. &:after {
  83. position: absolute;
  84. content: '';
  85. width: 0;
  86. height: 0;
  87. border-bottom: 6rpx solid #00B38B;
  88. left: 50%;
  89. bottom: 18rpx;
  90. z-index: -99;
  91. height: 16rpx;
  92. background: #00b38b;
  93. opacity: 0.3;
  94. transform: translateX(-50%);
  95. transition: .3s;
  96. }
  97. }
  98. .active {
  99. // color: #00B38B;
  100. font-weight: bold;
  101. font-size:30rpx;
  102. &:after {
  103. width: 70%;
  104. border-radius: 6rpx;
  105. }
  106. &:first-child{
  107. &:after {
  108. width:50%;
  109. }
  110. }
  111. &:last-child{
  112. &:after {
  113. width: 95%;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. </style>