u-cate-tab.vue 10 KB

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