cate.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <view class="category">
  3. <view class="category-wrapper" v-if="fetch.cate.length>0">
  4. <!-- 左边导航 -->
  5. <scroll-view scroll-y="true" class="left-wrapper" scroll-with-animation="true" :scroll-top="left_scroll">
  6. <view class="left-content">
  7. <view class="title-content" :class="select_index === k?'onSelected':''" v-for="(v,k) in fetch.cate" :key="k" @tap="choose(k)">{{v.name}}</view>
  8. </view>
  9. </scroll-view>
  10. <!-- 右边内容 -->
  11. <scroll-view scroll-y="true" class="right-wrapper" scroll-with-animation="true" :scroll-top="right_scroll" @scroll="myscroll">
  12. <view class="right-content">
  13. <!-- banner区域 -->
  14. <view class="banner-wrapper" v-if="swiperList.length > 0">
  15. <swiper class="swiper-content" :autoplay="true" :interval="3000" :circular="true">
  16. <swiper-item class="swiper-item" v-for="imgs in swiperList" :key="imgs.id">
  17. <image class="swiper-img" :src="imgs.src"></image>
  18. </swiper-item>
  19. </swiper>
  20. </view>
  21. <!-- 产品区 -->
  22. <view class="product-wrapper">
  23. <view class="category-item" :id="'list'+k" v-for="(v,k) in fetch.child" :key="k" v-if="v.show == 1">
  24. <view class="category-title">{{v.name}}</view>
  25. <view class="category-content">
  26. <view class="product-item" v-for="(v1,k1) in v.content" :key="k1">
  27. <image class="product-img" :src="v1.pic"></image>
  28. <text class="product-title">{{v1.cname}}</text>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </scroll-view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. data() {
  41. return {
  42. fetch: {
  43. cate : []
  44. },
  45. windows_height: 0, //屏幕高度
  46. swiperList: [],
  47. catrgoryList: [],
  48. select_index: 0,
  49. right_height: [], //右侧每个内容的高度集合
  50. right_scroll: 0, //右侧的滑动值
  51. left_height: 0, //左侧总高度
  52. left_scroll: 0, //左侧滑动值
  53. last: 0,
  54. }
  55. },
  56. onLoad(option) {
  57. this.id = option.id;
  58. this.getData();
  59. this.windows_height = uni.getSystemInfoSync().windowHeight;
  60. },
  61. methods: {
  62. getData : function() {
  63. this.Dever.get(this, 'app/collection/?l=api.category', {id:this.id});
  64. },
  65. init : function() {
  66. uni.request({
  67. url: 'https://www.easy-mock.com/mock/5d351e87b5e1f213739d6498/shop/categoryList', //仅为示例,并非真实接口地址。
  68. method: 'GET',
  69. success: (res) => {
  70. if (res.data.error === 0) {
  71. this.catrgoryList = res.data.data.list;
  72. this.swiperList = res.data.data.banner;
  73. this.$nextTick(() => {
  74. this.getHeightList();
  75. })
  76. }
  77. }
  78. });
  79. },
  80. getHeightList : function() {
  81. let _this = this;
  82. let selectorQuery = uni.createSelectorQuery();
  83. selectorQuery.select('.left-content').boundingClientRect(function(rects) {
  84. _this.left_height = rects.height;
  85. });
  86. selectorQuery.selectAll('.category-item').boundingClientRect(function(rects) {
  87. _this.right_height = rects.map((item) => item.top);
  88. console.log(_this.right_height)
  89. }).exec();
  90. },
  91. choose : function(index) {
  92. if (this.select_index === index) {
  93. return;
  94. }
  95. this.select_index = index;
  96. // 加入防抖
  97. if (this.timeout) {
  98. clearTimeout(this.timeout); //清除计时器
  99. }
  100. this.timeout = setTimeout(() => {
  101. this.right_scroll = this.right_height[index] - 110;
  102. }, 300)
  103. },
  104. myscroll : function(e) {
  105. //引入节流
  106. let right_content_height = e.detail.scrollHeight - this.windows_height;
  107. if (right_content_height == e.detail.scrollTop) {
  108. return;
  109. }
  110. let scroll_top = e.detail.scrollTop + 110; //110是banner图的高度
  111. //判断当前的scrollTop在哪个区间内;
  112. let now = +new Date();
  113. if (now - this.last > 100) {
  114. this.last = now;
  115. let active_index = this.right_height.findIndex((value, index, arr) => value <= scroll_top && scroll_top < arr[index + 1]);
  116. this.select_index = active_index;
  117. if (this.left_height - this.windows_height) {
  118. //如果有超出部分
  119. let diff = this.left_height - this.windows_height
  120. this.left_scroll = Math.round((active_index * diff) / (this.catrgoryList.length - 1))
  121. }
  122. }
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="less">
  128. .category {
  129. .category-wrapper {
  130. width: 100%;
  131. display: flex;
  132. flex-direction: row;
  133. position: absolute;
  134. top: 0;
  135. bottom: 0;
  136. .left-wrapper {
  137. width: 200rpx;
  138. flex: 0 0 200rpx;
  139. background-color: #f6f6f6;
  140. .left-content {
  141. .title-content {
  142. width: 100%;
  143. height: 100rpx;
  144. display: flex;
  145. justify-content: center;
  146. align-items: center;
  147. font-size: 25rpx;
  148. border-bottom: 1px solid #dedede;
  149. cursor: pointer;
  150. &:last-child {
  151. border-bottom: 0;
  152. }
  153. &.onSelected {
  154. background-color: #fff;
  155. position: relative;
  156. color: #feb436;
  157. &::before {
  158. content: '';
  159. position: absolute;
  160. left: 0;
  161. top: 0;
  162. width: 10rpx;
  163. height: 100%;
  164. background: linear-gradient(124deg, #feb436 0%, #fb7c22 100%);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. .right-wrapper {
  171. flex: 1;
  172. .right-content {
  173. width: 100%;
  174. padding: 20rpx 0;
  175. border-left: 1rpx solid #efefef;
  176. box-sizing: border-box;
  177. .banner-wrapper {
  178. padding: 0 20rpx;
  179. .swiper-content {
  180. width: 100%;
  181. height: 180rpx;
  182. margin-bottom: 20rpx;
  183. .swiper-item {
  184. .swiper-img {
  185. width: 100%;
  186. height: 180rpx;
  187. }
  188. }
  189. }
  190. }
  191. .product-wrapper {
  192. .category-item {
  193. .category-title {
  194. height: 60rpx;
  195. font-size: 26rpx;
  196. line-height: 60rpx;
  197. font-weight: 500;
  198. background-color: #f6f6f6;
  199. padding-left: 20rpx;
  200. color: #000;
  201. }
  202. .category-content {
  203. display: flex;
  204. flex-direction: row;
  205. flex-flow: wrap;
  206. padding: 20rpx 20rpx 0;
  207. .product-item {
  208. width: 33%;
  209. display: flex;
  210. flex-direction: column;
  211. justify-content: center;
  212. align-items: center;
  213. margin-bottom: 20rpx;
  214. .product-img {
  215. width: 120rpx;
  216. height: 140rpx;
  217. margin-bottom: 10rpx;
  218. }
  219. .product-title {
  220. font-size: 23rpx;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. </style>