u-cate-tab.vue 9.2 KB

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