u-cate-tab.vue 9.4 KB

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