mallMenu.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 tabbar" :key="index" class="u-tab-item"
  7. :class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
  8. <text class="u-line-1">{{ item.businessName }}</text>
  9. </view>
  10. </scroll-view>
  11. <scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box"
  12. @scroll="rightScroll">
  13. <view class="page-view">
  14. <view class="class-item" :id="'item' + index" v-for="(item, index) in tabbar" :key="index">
  15. <view class="item-title">
  16. <text>{{ item.businessName }}</text>
  17. </view>
  18. <view class="item-container">
  19. <view class="thumb-box"
  20. v-for="(item1, index1) in (item.children?item.children:[item])"
  21. :key="index1" @click="clickMenu(item,item1)">
  22. <view class="item-menu-name">{{ item1.businessName }}</view>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import { getTreeList } from '@/api/volunteer'
  33. import { volunteerSeachgetTreeList } from "@/api/volunteerDetailsApi/details.js"
  34. import { getVolunteerInfo } from '@/api/volunteer.js'
  35. export default {
  36. data() {
  37. return {
  38. scrollTop: 0, //tab标题的滚动条位置
  39. oldScrollTop: 0,
  40. current: 0, // 预设当前项的值
  41. menuHeight: 0, // 左边菜单的高度
  42. menuItemHeight: 0, // 左边菜单item的高度
  43. itemId: '', // 栏目右边scroll-view用于滚动的id
  44. tabbar: [],
  45. arr: [],
  46. scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
  47. timer: null, // 定时器
  48. userType: uni.getStorageSync('userType')
  49. }
  50. },
  51. onReady() {
  52. this.getData();
  53. this.getMenuItemTop()
  54. },
  55. methods: {
  56. getData() {
  57. getTreeList({ parentId: '0' }).then(res => {
  58. this.tabbar = res.data;
  59. })
  60. },
  61. // 点击左边的栏目切换
  62. async swichMenu(index) {
  63. if (this.arr.length == 0) {
  64. await this.getMenuItemTop();
  65. }
  66. if (index == this.current) return;
  67. this.scrollRightTop = this.oldScrollTop;
  68. this.$nextTick(() => {
  69. this.scrollRightTop = this.arr[index];
  70. this.current = index;
  71. this.leftMenuStatus(index);
  72. })
  73. },
  74. // 获取一个目标元素的高度
  75. getElRect(elClass, dataVal) {
  76. new Promise((resolve, reject) => {
  77. const query = uni.createSelectorQuery().in(this);
  78. query.select('.' + elClass).fields({
  79. size: true
  80. }, res => {
  81. // 如果节点尚未生成,res值为null,循环调用执行
  82. if (!res) {
  83. setTimeout(() => {
  84. this.getElRect(elClass);
  85. }, 10);
  86. return;
  87. }
  88. this[dataVal] = res.height;
  89. resolve();
  90. }).exec();
  91. })
  92. },
  93. // 观测元素相交状态
  94. async observer() {
  95. this.tabbar.map((val, index) => {
  96. let observer = uni.createIntersectionObserver(this);
  97. // 检测右边scroll-view的id为itemxx的元素与right-box的相交状态
  98. // 如果跟.right-box底部相交,就动态设置左边栏目的活动状态
  99. observer.relativeTo('.right-box', {
  100. top: 0
  101. }).observe('#item' + index, res => {
  102. if (res.intersectionRatio > 0) {
  103. let id = res.id.substring(4);
  104. this.leftMenuStatus(id);
  105. }
  106. })
  107. })
  108. },
  109. // 设置左边菜单的滚动状态
  110. async leftMenuStatus(index) {
  111. this.current = index;
  112. // 如果为0,意味着尚未初始化
  113. if (this.menuHeight == 0 || this.menuItemHeight == 0) {
  114. await this.getElRect('menu-scroll-view', 'menuHeight');
  115. await this.getElRect('u-tab-item', 'menuItemHeight');
  116. }
  117. // 将菜单活动item垂直居中
  118. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  119. },
  120. // 获取右边菜单每个item到顶部的距离
  121. getMenuItemTop() {
  122. new Promise(resolve => {
  123. let selectorQuery = uni.createSelectorQuery();
  124. selectorQuery.selectAll('.class-item').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. }).exec()
  138. })
  139. },
  140. // 右边菜单滚动
  141. async rightScroll(e) {
  142. this.oldScrollTop = e.detail.scrollTop;
  143. if (this.arr.length == 0) {
  144. await this.getMenuItemTop();
  145. }
  146. if (this.timer) return;
  147. if (!this.menuHeight) {
  148. await this.getElRect('menu-scroll-view', 'menuHeight');
  149. }
  150. setTimeout(() => { // 节流
  151. this.timer = null;
  152. // scrollHeight为右边菜单垂直中点位置
  153. let scrollHeight = e.detail.scrollTop + this.menuHeight / 2;
  154. for (let i = 0; i < this.arr.length; i++) {
  155. let height1 = this.arr[i];
  156. let height2 = this.arr[i + 1];
  157. // 如果不存在height2,意味着数据循环已经到了最后一个,设置左边菜单为最后一项即可
  158. if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
  159. this.leftMenuStatus(i);
  160. return;
  161. }
  162. }
  163. }, 10)
  164. },
  165. async clickMenu(service,record) {
  166. console.log(service,record);
  167. const { id } = service;
  168. const key = id;
  169. if (this.userType == '2') {
  170. const res = await getVolunteerInfo({
  171. serviceCategory: key
  172. });
  173. const parmas = { ...service, key,name:service.businessName,record };
  174. if (res.code === 200 && res.data) {
  175. //已有注册,跳转详情页面
  176. uni.navigateTo({
  177. url: `/pages_home/pages/details/index?data=${encodeURIComponent(JSON.stringify(parmas))}`
  178. })
  179. return
  180. }
  181. [1, 2] ? uni.navigateTo({
  182. url: `/pages_home/pages/register/index?data=${encodeURIComponent(JSON.stringify(parmas))}`
  183. }) : uni.showToast({
  184. title: '敬请期待',
  185. icon: 'none'
  186. })
  187. return;
  188. }
  189. // 动态获取 parentId
  190. const params = {
  191. parentId: key
  192. }
  193. const res = await volunteerSeachgetTreeList(params)
  194. // 只有第一条和第二条可以跳转
  195. if (key === '1' || key === '2') {
  196. uni.navigateTo({
  197. url: `/pages_home/pages/client/details?dataList=${encodeURIComponent(JSON.stringify(res.data))}`
  198. });
  199. } else {
  200. // 其他条目提示“敬请期待”
  201. uni.showToast({
  202. title: '敬请期待',
  203. icon: 'none'
  204. });
  205. }
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss" scoped>
  211. .u-wrap {
  212. height: calc(100vh);
  213. /* #ifdef H5 */
  214. height: calc(100vh - var(--window-top));
  215. /* #endif */
  216. display: flex;
  217. flex-direction: column;
  218. }
  219. .u-search-box {
  220. padding: 18rpx 30rpx;
  221. }
  222. .u-menu-wrap {
  223. flex: 1;
  224. display: flex;
  225. overflow: hidden;
  226. }
  227. .u-search-inner {
  228. background-color: rgb(234, 234, 234);
  229. border-radius: 100rpx;
  230. display: flex;
  231. align-items: center;
  232. padding: 10rpx 16rpx;
  233. }
  234. .u-search-text {
  235. font-size: 26rpx;
  236. color: $u-tips-color;
  237. margin-left: 10rpx;
  238. }
  239. .u-tab-view {
  240. width: 200rpx;
  241. height: 100%;
  242. }
  243. .u-tab-item {
  244. height: 110rpx;
  245. background: #f6f6f6;
  246. box-sizing: border-box;
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. font-size: 26rpx;
  251. color: #444;
  252. font-weight: 400;
  253. line-height: 1;
  254. }
  255. .u-tab-item-active {
  256. position: relative;
  257. color: rgba(221, 94, 69, 1);
  258. font-size: 30rpx;
  259. font-weight: 600;
  260. background: #fff;
  261. }
  262. .u-tab-item-active::before {
  263. border-left: 4px solid rgba(221, 94, 69, 1) !important;
  264. }
  265. .u-tab-item-active::before {
  266. content: "";
  267. position: absolute;
  268. border-left: 4px solid $u-primary;
  269. height: 32rpx;
  270. left: 0;
  271. top: 39rpx;
  272. }
  273. .u-tab-view {
  274. height: 100%;
  275. }
  276. .right-box {
  277. background-color: rgb(250, 250, 250);
  278. }
  279. .page-view {
  280. padding: 16rpx;
  281. }
  282. .class-item {
  283. margin-bottom: 30rpx;
  284. background-color: #fff;
  285. padding: 16rpx;
  286. border-radius: 8rpx;
  287. }
  288. .class-item:last-child {
  289. min-height: 100vh;
  290. }
  291. .item-title {
  292. font-size: 26rpx;
  293. color: $u-main-color;
  294. font-weight: bold;
  295. text-align: center;
  296. }
  297. .item-menu-name {
  298. font-weight: normal;
  299. font-size: 28rpx;
  300. color: $u-main-color;
  301. }
  302. .item-container {
  303. // display: flex;
  304. // flex-wrap: wrap;
  305. display: grid;
  306. grid-template-columns: repeat(3, 1fr);
  307. gap: 12rpx;
  308. margin: 24rpx 0;
  309. }
  310. .thumb-box {
  311. // width: 33.333333%;
  312. display: flex;
  313. align-items: center;
  314. justify-content: center;
  315. flex-direction: column;
  316. // margin-top: 20rpx;
  317. background: #f5f5f5;
  318. padding: 12rpx 0;
  319. }
  320. .item-menu-image {
  321. width: 120rpx;
  322. height: 120rpx;
  323. }
  324. </style>