u-cate-tab.vue 8.6 KB

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