Menu.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view class="menu-list-box" v-if="carousel" :style="list.length <= menu ? `height:160rpx` : `height:320rpx`">
  3. <swiper
  4. class="menu-swiper-box"
  5. :style="list.length <= menu ? `height:160rpx` : `height:320rpx`"
  6. @change="onSwiper"
  7. circular
  8. :autoplay="false"
  9. :interval="3000"
  10. :duration="1000">
  11. <swiper-item class="menu-swiper-item" v-for="(itemList, index) in carousel" :key="index" :style="list.length <= menu ? `height:200rpx` : `height:340rpx`">
  12. <view class="menu-tab-box">
  13. <view class="tab-list" v-for="item in itemList" :key="item.cateId" @tap="routerTo(item)">
  14. <image class="tab-img" :style="{ width: imgW + 'rpx', height: imgW + 'rpx' }" :src="item.imgUrl"></image>
  15. <text :class="cateId == item.cateId?'tab-title active':'tab-title'" >{{ item.cateName }}</text>
  16. </view>
  17. </view>
  18. </swiper-item>
  19. </swiper>
  20. <view class="menu-dots" v-if="carousel.length > 1">
  21. <text :class="menuCurrent === index ? 'dot-active' : 'dot'" v-for="(dot, index) in carousel.length" :key="index"></text>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. components: {},
  28. data() {
  29. return {
  30. cateId:0,
  31. menuCurrent: 0 //轮播下标
  32. };
  33. },
  34. props: {
  35. list: {
  36. type: Array,
  37. default: []
  38. },
  39. menu: {
  40. default: 4
  41. },
  42. imgW: {
  43. type: Number,
  44. default: 88
  45. }
  46. },
  47. computed: {
  48. carousel() {
  49. if (this.list) {
  50. let data = this.sortData(this.list, this.menu * 2);
  51. return data;
  52. }
  53. }
  54. },
  55. created() {},
  56. methods: {
  57. // 数据分层
  58. sortData(oArr, length) {
  59. let arr = [];
  60. let minArr = [];
  61. oArr.forEach(c => {
  62. if (minArr.length === length) {
  63. minArr = [];
  64. }
  65. if (minArr.length === 0) {
  66. arr.push(minArr);
  67. }
  68. minArr.push(c);
  69. });
  70. return arr;
  71. },
  72. // 轮播
  73. onSwiper(e) {
  74. this.menuCurrent = e.detail.current;
  75. },
  76. routerTo(item) {
  77. this.cateId=item.cateId;
  78. this.$emit('menuClick',item);
  79. },
  80. setCateId(cateId){
  81. this.cateId=cateId;
  82. }
  83. }
  84. };
  85. </script>
  86. <style lang="scss">
  87. .menu-list-box {
  88. padding: 0rpx;
  89. background: #fff;
  90. box-sizing: border-box;
  91. }
  92. .menu-list-box,
  93. .menu-swiper-box {
  94. position: relative;
  95. background: #fff;
  96. .menu-swiper-item {
  97. background: #fff;
  98. height: 100%;
  99. width: 100%;
  100. }
  101. .menu-tab-box {
  102. display: flex;
  103. flex-wrap: wrap;
  104. .tab-list {
  105. width: 25%;
  106. display: -webkit-box;
  107. display: -webkit-flex;
  108. display: flex;
  109. -webkit-box-orient: vertical;
  110. -webkit-box-direction: normal;
  111. -webkit-flex-direction: column;
  112. flex-direction: column;
  113. -webkit-box-align: center;
  114. -webkit-align-items: center;
  115. align-items: center;
  116. font-size: 22rpx;
  117. font-family: PingFang SC;
  118. font-weight: 500;
  119. color: rgba(51, 51, 51, 1);
  120. padding-bottom: 30rpx;
  121. .tab-img {
  122. border-radius: 25rpx;
  123. margin-bottom: 10rpx;
  124. }
  125. .tab-title{
  126. font-size: 24upx;
  127. font-family: PingFang SC;
  128. font-weight: 500;
  129. color: #111;
  130. margin-top: 10rpx;
  131. }
  132. .active{
  133. color: #2583EB;
  134. }
  135. }
  136. }
  137. .menu-dots {
  138. display: flex;
  139. position: absolute;
  140. left: 50%;
  141. transform: translateX(-50%);
  142. .dot {
  143. width: 40rpx;
  144. height: 3rpx;
  145. background: #eeeeee;
  146. margin-right: 10rpx;
  147. }
  148. .dot-active {
  149. width: 40rpx;
  150. height: 3rpx;
  151. background: #2583EB;
  152. margin-right: 10rpx;
  153. }
  154. }
  155. }
  156. </style>