list.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="container">
  3. <!-- 顶部 Tab 切换 -->
  4. <scroll-view scroll-x class="tab-scroll">
  5. <view class="tabs">
  6. <view
  7. v-for="(tab, index) in tabList"
  8. :key="index"
  9. :class="['tab', currentSwiperIndex === index ? 'active' : '']"
  10. @click="onTabChange(index)"
  11. >
  12. {{ tab.name }}
  13. </view>
  14. </view>
  15. </scroll-view>
  16. <!-- Swiper 内容区 -->
  17. <swiper
  18. :current="currentSwiperIndex"
  19. @change="onSwiperChange"
  20. class="swiper-container"
  21. >
  22. <swiper-item v-for="(tab, tabIndex) in tabList" :key="tabIndex">
  23. <!-- 单个列表页 -->
  24. <scroll-view
  25. scroll-y
  26. class="list-scroll"
  27. @scroll="(e) => onListScroll(tabIndex, e)"
  28. :scroll-top="tab.scrollTop"
  29. >
  30. <view class="list">
  31. <view
  32. v-for="(item, itemIndex) in tab.listData"
  33. :key="itemIndex"
  34. class="list-item"
  35. >
  36. {{ item }}
  37. </view>
  38. </view>
  39. </scroll-view>
  40. </swiper-item>
  41. </swiper>
  42. </view>
  43. </template>
  44. <script>
  45. export default {
  46. data() {
  47. return {
  48. currentSwiperIndex: 0, // 当前显示的 swiper-item 索引
  49. tabList: [ // 所有 Tab 及其对应的列表数据
  50. {
  51. name: '标签1',
  52. listData: ['项目1-1', '项目1-2', '项目1-3', /* ...更多数据... */],
  53. scrollTop: 0 // 记录该列表的滚动位置
  54. },
  55. {
  56. name: '标签2',
  57. listData: ['项目2-1', '项目2-2', '项目2-3', /* ... */],
  58. scrollTop: 0
  59. },
  60. // ...更多 Tab...
  61. ]
  62. };
  63. },
  64. methods: {
  65. // 点击 Tab 切换
  66. onTabChange(index) {
  67. this.currentSwiperIndex = index;
  68. },
  69. // Swiper 滑动切换
  70. onSwiperChange(e) {
  71. this.currentSwiperIndex = e.detail.current;
  72. },
  73. // 记录每个列表的滚动位置
  74. onListScroll(tabIndex, e) {
  75. this.tabList[tabIndex].scrollTop = e.detail.scrollTop;
  76. }
  77. }
  78. };
  79. </script>
  80. <style>
  81. .container {
  82. display: flex;
  83. flex-direction: column;
  84. height: 100vh;
  85. }
  86. .tab-scroll {
  87. white-space: nowrap;
  88. background-color: #f8f8f8;
  89. }
  90. .tabs {
  91. display: inline-flex;
  92. }
  93. .tab {
  94. padding: 20rpx 40rpx;
  95. }
  96. .tab.active {
  97. color: #007AFF;
  98. border-bottom: 4rpx solid #007AFF;
  99. }
  100. .swiper-container {
  101. flex: 1;
  102. }
  103. .list-scroll {
  104. height: 100%;
  105. }
  106. .list-item {
  107. padding: 30rpx;
  108. border-bottom: 1rpx solid #eee;
  109. }
  110. </style>