category.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view class="u-wrap">
  3. <view class="u-menu-wrap">
  4. <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop"
  5. :scroll-into-view="itemId">
  6. <view v-for="(item, index) in left_first_list" :key="index" class="u-tab-item"
  7. :class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="handleLeftMenuClick(index)">
  8. <text class="u-line-1">{{ item.name }}</text>
  9. </view>
  10. </scroll-view>
  11. <scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box" @scroll="rightScroll">
  12. <view class="page-view">
  13. <view class="class-item" :id="'item' + index" v-for="(item, index) in left_first_list" :key="index">
  14. <view class="item-title">
  15. <text>{{ item.name }}</text>
  16. </view>
  17. <view class="item-container">
  18. <view class="thumb-box" v-for="(child, childIndex) in item.children" :key="childIndex"
  19. @click="handleItemClick(child.id, child.name)">
  20. <image v-if="child.icon != ''" class="item-menu-image" :src="child.icon" mode=""></image>
  21. <view v-else class="item-menu-image row-c" style="background-color: #f4f6f8">
  22. <text style="font-size: 20rpx; color: #d0d0d0">加载失败</text>
  23. </view>
  24. <view class="item-menu-name">{{ child.name }}</view>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </scroll-view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. // import {
  35. // buildTree
  36. // } from '@/utils/tools.js';
  37. import {
  38. maintainCategories
  39. } from '@/api/client/business.js';
  40. export default {
  41. data() {
  42. return {
  43. scrollTop: 0, //tab标题的滚动条位置
  44. oldScrollTop: 0, // tab标题的滚动条旧位置
  45. current: 0, // 预设当前项的值
  46. menuHeight: 0, // 左边菜单的高度
  47. menuItemHeight: 0, // 左边菜单item的高度
  48. itemId: '', // 栏目右边scroll-view用于滚动的id
  49. cateList: [], // 渲染的数据
  50. arr: [], // 储存距离顶部高度的数组
  51. scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
  52. timer: null, // 定时器
  53. left_first_list: [], //左侧一级列表
  54. };
  55. },
  56. onLoad() {
  57. // this.cateList = buildTree(this.getCache('categories'));
  58. },
  59. onReady() {
  60. this.getMenuItemTop();
  61. },
  62. mounted() {
  63. // 获取左侧一级列表信息
  64. maintainCategories().then(res => {
  65. console.log('@@@@res',res);
  66. this.left_first_list = res.data;
  67. });
  68. },
  69. methods: {
  70. /**
  71. * 获取一个目标元素的高度
  72. * @elClass 元素的类名
  73. * @dataVal 储存高度的对象
  74. */
  75. getElRect(elClass, dataVal) {
  76. new Promise((resolve, reject) => {
  77. const query = uni.createSelectorQuery().in(this);
  78. query
  79. .select('.' + elClass)
  80. .fields({
  81. size: true,
  82. },
  83. res => {
  84. // 如果节点尚未生成,res值为null,循环调用执行
  85. if (!res) {
  86. setTimeout(() => {
  87. this.getElRect(elClass);
  88. }, 10);
  89. return;
  90. }
  91. this[dataVal] = res.height;
  92. resolve();
  93. },
  94. )
  95. .exec();
  96. });
  97. },
  98. /**
  99. * 获取右边菜单每个item到顶部的距离
  100. * 储存到 arr 数组里面用于后面滚动判断
  101. */
  102. getMenuItemTop() {
  103. new Promise(resolve => {
  104. let selectorQuery = uni.createSelectorQuery();
  105. selectorQuery
  106. .selectAll('.class-item')
  107. .boundingClientRect(rects => {
  108. // 如果节点尚未生成,rects值为[](因为用selectAll,所以返回的是数组),循环调用执行
  109. if (!rects.length) {
  110. setTimeout(() => {
  111. this.getMenuItemTop();
  112. }, 10);
  113. return;
  114. }
  115. rects.forEach(rect => {
  116. // 视情况而定,这里减去rects[0].top,是因为第一项顶部可能不是贴到导航栏(比如有个搜索框的情况)
  117. // this.arr.push(rect.top - rects[0].top);
  118. this.arr.push(rect.top);
  119. resolve();
  120. });
  121. })
  122. .exec();
  123. });
  124. },
  125. /**
  126. * 观测元素相交状态
  127. * 检测右边scroll-view的id为itemxx的元素与right-box的相交状态
  128. * 如果跟.right-box底部相交,就动态设置左边栏目的活动状态
  129. */
  130. async observer() {
  131. this.cateList.map((val, index) => {
  132. let observer = uni.createIntersectionObserver(this);
  133. observer
  134. .relativeTo('.right-box', {
  135. top: 0,
  136. })
  137. .observe('#item' + index, res => {
  138. if (res.intersectionRatio > 0) {
  139. let id = res.id.substring(4);
  140. this.leftMenuStatus(id);
  141. }
  142. });
  143. });
  144. },
  145. /**
  146. * 设置左边菜单的滚动状态
  147. * @index 传入的 ID
  148. */
  149. async leftMenuStatus(index) {
  150. this.current = index;
  151. // 如果为0,意味着尚未初始化
  152. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  153. await this.getElRect('menu-scroll-view', 'menuHeight');
  154. await this.getElRect('u-tab-item', 'menuItemHeight');
  155. }
  156. // 将菜单活动item垂直居中
  157. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  158. },
  159. /**
  160. * 点击左边的栏目切换
  161. * @index 传入的 ID
  162. */
  163. async handleLeftMenuClick(index) {
  164. if (this.arr.length == 0) {
  165. await this.getMenuItemTop();
  166. }
  167. if (index == this.current) return;
  168. this.scrollRightTop = this.oldScrollTop;
  169. this.$nextTick(function() {
  170. this.scrollRightTop = this.arr[index];
  171. this.current = index;
  172. this.leftMenuStatus(index);
  173. });
  174. },
  175. /**
  176. * 右边菜单滚动
  177. * 如果不存在height2,意味着数据循环已经到了最后一个,设置左边菜单为最后一项即可
  178. */
  179. async rightScroll(e) {
  180. this.oldScrollTop = e.detail.scrollTop;
  181. if (this.arr.length == 0) {
  182. await this.getMenuItemTop();
  183. }
  184. if (this.timer) return;
  185. if (!this.menuHeight) {
  186. await this.getElRect('menu-scroll-view', 'menuHeight');
  187. }
  188. setTimeout(() => {
  189. // 节流
  190. this.timer = null;
  191. // scrollHeight为右边菜单垂直中点位置
  192. // let scrollHeight = e.detail.scrollTop + this.menuHeight / 2;
  193. // scrollHeight为右边菜单头部位置
  194. let scrollHeight = e.detail.scrollTop + 20;
  195. for (let i = 0; i < this.arr.length; i++) {
  196. let height1 = this.arr[i];
  197. let height2 = this.arr[i + 1];
  198. if (!height2 || (scrollHeight >= height1 && scrollHeight < height2)) {
  199. this.leftMenuStatus(i);
  200. return;
  201. }
  202. }
  203. }, 10);
  204. },
  205. handleItemClick(id, name) {
  206. this.$Router.push(`/pages/client/clientPackage/storeList?id=${id}`);
  207. },
  208. },
  209. };
  210. </script>
  211. <style scoped>
  212. .u-wrap {
  213. /* #ifdef H5 */
  214. height: calc(100vh - var(--window-top));
  215. /* #endif */
  216. display: flex;
  217. flex-direction: column;
  218. height: 100vh;
  219. }
  220. .u-search-box {
  221. padding: 18rpx 30rpx;
  222. }
  223. .u-menu-wrap {
  224. flex: 1;
  225. display: flex;
  226. overflow: hidden;
  227. }
  228. .u-tab-view {
  229. width: 200rpx;
  230. height: 100%;
  231. }
  232. .u-tab-item {
  233. height: 110rpx;
  234. background: #f6f6f6;
  235. box-sizing: border-box;
  236. display: flex;
  237. align-items: center;
  238. justify-content: center;
  239. font-size: 26rpx;
  240. color: #444;
  241. font-weight: 400;
  242. line-height: 1;
  243. }
  244. .u-tab-item-active {
  245. position: relative;
  246. color: #06a446;
  247. font-size: 30rpx;
  248. font-weight: 500;
  249. background: #d6ffe7;
  250. }
  251. .u-tab-item-active::before {
  252. content: '';
  253. position: absolute;
  254. border-left: 4px solid #06a446;
  255. height: 52rpx;
  256. left: 0;
  257. top: 29rpx;
  258. }
  259. .u-tab-view {
  260. height: 100%;
  261. }
  262. .right-box {
  263. background-color: rgb(250, 250, 250);
  264. }
  265. .page-view {
  266. padding: 16rpx;
  267. }
  268. .class-item {
  269. margin-bottom: 30rpx;
  270. background-color: #fff;
  271. padding: 16rpx;
  272. border-radius: 8rpx;
  273. }
  274. .class-item:last-child {
  275. min-height: 100vh;
  276. }
  277. .item-title {
  278. font-size: 26rpx;
  279. color: #06a446;
  280. font-weight: bold;
  281. }
  282. .item-menu-name {
  283. margin-top: 8rpx;
  284. font-weight: normal;
  285. font-size: 24rpx;
  286. color: #06a446;
  287. }
  288. .item-container {
  289. display: flex;
  290. flex-wrap: wrap;
  291. }
  292. .thumb-box {
  293. width: 33.333333%;
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. flex-direction: column;
  298. margin-top: 20rpx;
  299. }
  300. .item-menu-image {
  301. width: 120rpx;
  302. height: 120rpx;
  303. }
  304. </style>