u-cate-tab.vue 10 KB

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