u-cate-tab.vue 10 KB

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