CateThree.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <view class="clearfix" :class="['qn-page-' + theme]">
  3. <view class="float_left">
  4. <scroll-view
  5. scroll-with-animation
  6. scroll-y
  7. class="left-aside"
  8. :style="{ height: 'calc(100vh - 112rpx - ' + (isBang ? '84px' : '50px') + ')' }"
  9. :scroll-top="scrollTop"
  10. :scroll-into-view="itemId"
  11. >
  12. <view v-for="(item, index) in cate_list" :key="index" class="f-item b-b ellipsis" :class="{ active: index === current_index }" @click="tabtap(index, item)">
  13. <view v-if="index === current_index" class="active-line primary-bg"></view>
  14. {{ item.title }}
  15. </view>
  16. </scroll-view>
  17. </view>
  18. <view class="float_right">
  19. <scroll-view
  20. :scroll-top="scrollRightTop"
  21. @scroll="rightScroll"
  22. scroll-with-animation
  23. scroll-y
  24. class="right-aside"
  25. :style="{ height: 'calc(100vh - 112rpx - ' + (isBang ? '84px' : '50px') + ')' }"
  26. >
  27. <view v-for="(titem, tindex) in cate_list" :key="tindex" class="cate-ul" :id="'item' + tindex">
  28. <view class="cate-name">{{ titem.title }}</view>
  29. <view class="clearfix">
  30. <view class="cate-li" @click="goPage('/pagesT/productDetail/productDetail?id=' + titem.id + '&name=' + titem.title)">
  31. <view class="cate-img-view">
  32. <image v-if="titem.images" :src="titem.images" mode="aspectFill" class="cate-img"></image>
  33. <view class="ibonfont ibonleimupinleifenleileibie cate-img-icon" v-else></view>
  34. </view>
  35. <view class="cate-tit">{{ titem.title }}</view>
  36. </view>
  37. <view
  38. class="cate-li"
  39. v-for="(item, index) in titem.children"
  40. :key="index"
  41. @click="goPage('/pagesT/productDetail/productDetail?id=' + item.id + '&name=' + item.title)"
  42. >
  43. <view class="cate-img-view">
  44. <image v-if="item.images" :src="item.images" mode="aspectFill" class="cate-img"></image>
  45. <view class="ibonfont ibonleimupinleifenleileibie cate-img-icon" v-else></view>
  46. </view>
  47. <view class="cate-tit ellipsis">{{ item.title }}</view>
  48. </view>
  49. </view>
  50. </view>
  51. </scroll-view>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. export default {
  57. data() {
  58. return {
  59. cate_list: [],
  60. cate_Two_list: [],
  61. current_index: 0,
  62. scrollTop: 0, //tab标题的滚动条位置
  63. oldScrollTop: 0,
  64. itemId: '', // 栏目右边scroll-view用于滚动的id
  65. scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
  66. arr: [],
  67. timer: null, // 定时器
  68. menuHeight: 0, // 左边菜单的高度
  69. menuItemHeight: 0 // 左边菜单item的高度
  70. };
  71. },
  72. computed: {
  73. isBang() {
  74. return this.$_utils.modelmes();
  75. },
  76. userId() {
  77. return this.$store.state.userStatus.id;
  78. }
  79. },
  80. created() {
  81. this.getAllCategory();
  82. },
  83. methods: {
  84. // 分类列表 getAllCategory
  85. getAllCategory() {
  86. this.$u.api
  87. .getAllCategory({
  88. userCenterId: this.userId || 0
  89. })
  90. .then(({ data }) => {
  91. this.cate_list = data;
  92. });
  93. },
  94. // 点击左边的栏目切换
  95. async tabtap(index,row) {
  96. if (this.arr.length == 0) {
  97. await this.getMenuItemTop();
  98. }
  99. if (index == this.current_index) return;
  100. this.scrollRightTop = this.oldScrollTop;
  101. this.$nextTick(function() {
  102. this.scrollRightTop = this.arr[index];
  103. this.current_index = index;
  104. this.leftMenuStatus(index);
  105. });
  106. },
  107. // 设置左边菜单的滚动状态
  108. async leftMenuStatus(index) {
  109. this.current = index;
  110. // 如果为0,意味着尚未初始化
  111. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  112. await this.getElRect('left-aside', 'menuHeight');
  113. await this.getElRect('cate-two-li', 'menuItemHeight');
  114. }
  115. // 将菜单活动item垂直居中
  116. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  117. },
  118. // 获取右边菜单每个item到顶部的距离
  119. getMenuItemTop() {
  120. new Promise(resolve => {
  121. let selectorQuery = uni.createSelectorQuery().in(this);
  122. selectorQuery
  123. .selectAll('.cate-ul')
  124. .boundingClientRect(rects => {
  125. // 如果节点尚未生成,rects值为[](因为用selectAll,所以返回的是数组),循环调用执行
  126. if (!rects.length) {
  127. setTimeout(() => {
  128. this.getMenuItemTop();
  129. }, 10);
  130. return;
  131. }
  132. rects.forEach(rect => {
  133. // 这里减去rects[0].top,是因为第一项顶部可能不是贴到导航栏(比如有个搜索框的情况)
  134. this.arr.push(rect.top - rects[0].top);
  135. resolve();
  136. });
  137. })
  138. .exec();
  139. });
  140. },
  141. // 右边菜单滚动
  142. async rightScroll(e) {
  143. this.oldScrollTop = e.detail.scrollTop;
  144. if (this.arr.length == 0) {
  145. await this.getMenuItemTop();
  146. }
  147. if (this.timer) return;
  148. if (!this.menuHeight) {
  149. await this.getElRect('left-aside', 'menuHeight');
  150. }
  151. setTimeout(() => {
  152. // 节流
  153. this.timer = null;
  154. // scrollHeight为右边菜单垂直中点位置
  155. let scrollHeight = e.detail.scrollTop + this.menuHeight / 2;
  156. for (let i = 0; i < this.arr.length; i++) {
  157. let height1 = this.arr[i];
  158. let height2 = this.arr[i + 1];
  159. // 如果不存在height2,意味着数据循环已经到了最后一个,设置左边菜单为最后一项即可
  160. if (!height2 || (scrollHeight >= height1 && scrollHeight < height2)) {
  161. this.leftMenuStatus(i);
  162. return;
  163. }
  164. }
  165. }, 10);
  166. },
  167. // 获取一个目标元素的高度
  168. getElRect(elClass, dataVal) {
  169. new Promise((resolve, reject) => {
  170. const query = uni.createSelectorQuery().in(this);
  171. query
  172. .select('.' + elClass)
  173. .fields(
  174. {
  175. size: true
  176. },
  177. res => {
  178. // 如果节点尚未生成,res值为null,循环调用执行
  179. if (!res) {
  180. setTimeout(() => {
  181. this.getElRect(elClass);
  182. }, 10);
  183. return;
  184. }
  185. this[dataVal] = res.height;
  186. resolve();
  187. }
  188. )
  189. .exec();
  190. });
  191. }
  192. }
  193. };
  194. </script>
  195. <style lang="scss">
  196. .left-aside {
  197. width: 200upx;
  198. background-color: #f5f6f7;
  199. /* #ifdef H5||MP */
  200. height: calc(100vh - 130upx);
  201. /*#endif*/
  202. /* #ifdef APP-PLUS */
  203. height: 100vh;
  204. /*#endif*/
  205. .f-item {
  206. -webkit-line-clamp: 1;
  207. width: 100%;
  208. height: 80upx;
  209. line-height: 80rpx;
  210. padding-left: 48rpx;
  211. margin-bottom: 10rpx;
  212. font-size: 26upx;
  213. color: #4b4b4b;
  214. position: relative;
  215. &.active {
  216. color: #000000;
  217. font-size: 32upx;
  218. font-weight: bold;
  219. background-color: #ffffff;
  220. border-radius: 100rpx 0 0 100rpx;
  221. }
  222. .active-line {
  223. position: absolute;
  224. left: 28rpx;
  225. top: 50%;
  226. transform: translateY(-50%);
  227. height: 24upx;
  228. width: 4upx;
  229. border-radius: 4rpx;
  230. }
  231. }
  232. }
  233. .right-aside {
  234. /* #ifdef H5||MP */
  235. height: calc(100vh - 140upx);
  236. /*#endif*/
  237. /* #ifdef APP-PLUS */
  238. height: 100vh;
  239. /*#endif*/
  240. width: 550upx;
  241. .cate-name {
  242. padding-top: 20rpx;
  243. padding-left: 40rpx;
  244. font-size: 26rpx;
  245. font-weight: 600;
  246. color: #000000;
  247. }
  248. .cate-li {
  249. float: left;
  250. width: 130upx;
  251. margin-left: 40upx;
  252. text-align: center;
  253. margin-top: 20upx;
  254. .cate-img-view {
  255. width: 130rpx;
  256. height: 104rpx;
  257. background: #f5f7f7;
  258. border-radius: 8px;
  259. margin-bottom: 24rpx;
  260. .cate-img {
  261. width: 84upx;
  262. height: 84upx;
  263. border-radius: 8upx;
  264. display: block;
  265. margin: 0 auto;
  266. transform: translateY(10rpx);
  267. }
  268. .cate-img-icon {
  269. font-size: 80upx;
  270. width: 84upx;
  271. line-height: 84upx;
  272. color: #eeeeee;
  273. height: 84upx;
  274. border-radius: 8upx;
  275. margin: 0 auto;
  276. transform: translateY(10rpx);
  277. }
  278. }
  279. .cate-tit {
  280. font-size: 24upx;
  281. font-weight: 400;
  282. color: #3a3a3a;
  283. width: 100%;
  284. -webkit-line-clamp: 1;
  285. }
  286. }
  287. }
  288. </style>