category.vue 8.7 KB

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