index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view class="body-view">
  3. <!-- 使用scroll-view实现tabs滑动切换 -->
  4. <scroll-view class="top-menu-view" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
  5. <view class="menu-topic-view" v-for="(item,index) in tabs" :id="'tabNum'+item.id" :key="index"
  6. @click="swichMenu(index)">
  7. <view :class="currentTab==index ? 'menu-topic-act' : 'menu-topic'">
  8. <text class="menu-topic-text">{{item.name}}</text>
  9. <view class="menu-topic-bottom">
  10. <view class="menu-topic-bottom-color"></view>
  11. </view>
  12. </view>
  13. </view>
  14. </scroll-view>
  15. <!-- 内容 -->
  16. <swiper class="swiper-box-list" :current="currentTab" @change="swiperChange">
  17. <swiper-item class="swiper-topic-list" v-for="item in swiperDateList" :key="item.id">
  18. <view class="swiper-item">
  19. {{item.content}}
  20. </view>
  21. </swiper-item>
  22. </swiper>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. scrollLeft: 0,
  30. tabs: [{
  31. id: 1,
  32. name: '选项卡一'
  33. },
  34. {
  35. id: 2,
  36. name: '选项卡二'
  37. },
  38. {
  39. id: 3,
  40. name: '选项卡三'
  41. },
  42. {
  43. id: 4,
  44. name: '选项卡四'
  45. },
  46. {
  47. id: 5,
  48. name: '选项卡五'
  49. },
  50. {
  51. id: 6,
  52. name: '选项卡六'
  53. },
  54. {
  55. id: 7,
  56. name: '选项卡七'
  57. },
  58. ],
  59. currentTab: 0,
  60. tabCurrent: 'tabNum1',
  61. // Tab切换内容
  62. swiperDateList: [{
  63. id: 1,
  64. content: '选项卡1页面'
  65. },
  66. {
  67. id: 2,
  68. content: '选项卡2页面'
  69. },
  70. {
  71. id: 3,
  72. content: '选项卡3页面'
  73. },
  74. {
  75. id: 4,
  76. content: '选项卡4页面'
  77. },
  78. {
  79. id: 5,
  80. content: '选项卡5页面'
  81. },
  82. {
  83. id: 6,
  84. content: '选项卡6页面'
  85. },
  86. {
  87. id: 7,
  88. content: '选项卡7页面'
  89. },
  90. ],
  91. }
  92. },
  93. onLoad() {
  94. },
  95. methods: {
  96. swichMenu(id) {
  97. this.currentTab = id
  98. console.log(11, id)
  99. //this.tabCurrent = 'tabNum'+ id
  100. // 滑动swiper后,每个选项距离其父元素最左侧的距离
  101. this.scrollLeft = 0;
  102. for (let i = 0; i < id; i++) {
  103. this.scrollLeft += 60
  104. console.log(this.scrollLeft, 60, id)
  105. };
  106. },
  107. swiperChange(e) {
  108. console.log(22, e.detail.current)
  109. let index = e.detail.current
  110. this.swichMenu(index)
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped lang="scss">
  116. .body-view {
  117. width: 100%;
  118. overflow: hidden;
  119. }
  120. .top-menu-view {
  121. display: flex;
  122. // position: fixed;
  123. z-index: 100;
  124. //top: 84rpx;
  125. /* #ifdef H5 */
  126. // top: 84rpx;
  127. /* #endif */
  128. /* #ifndef H5 */
  129. top: 0rpx;
  130. /* #endif */
  131. left: 0;
  132. white-space: nowrap;
  133. width: 100%;
  134. background-color: #fceeee;
  135. height: 86rpx;
  136. line-height: 86rpx;
  137. border-top: 1rpx solid #d8dbe6;
  138. .menu-topic-view {
  139. display: inline-block;
  140. white-space: nowrap;
  141. height: 86rpx;
  142. position: relative;
  143. .menu-topic-text {
  144. font-size: 30rpx;
  145. color: #303133;
  146. padding: 10rpx 40rpx;
  147. font-weight: 500;
  148. }
  149. .menu-topic-bottom {
  150. position: absolute;
  151. bottom: 0;
  152. width: 100%;
  153. .menu-topic-bottom-color {
  154. width: 40rpx;
  155. height: 4rpx;
  156. }
  157. }
  158. .menu-topic-act .menu-topic-bottom {
  159. display: flex;
  160. justify-content: center;
  161. }
  162. .menu-topic-act .menu-topic-text {
  163. color: #ff0000;
  164. }
  165. .menu-topic-act .menu-topic-bottom-color {
  166. background: #ff0000;
  167. }
  168. }
  169. }
  170. .swiper-box-list {
  171. width: 95%;
  172. margin: 100rpx auto 30rpx;
  173. padding: 0rpx 40rpx;
  174. flex: 1;
  175. background-color: #FFFFFF;
  176. height: calc(100vh - 251rpx);
  177. .swiper-topic-list {
  178. width: 100%;
  179. }
  180. }
  181. /* 隐藏滚动条,但依旧具备可以滚动的功能 */
  182. /deep/.uni-scroll-view::-webkit-scrollbar {
  183. display: none
  184. }
  185. </style>