u-cate-tab.vue 9.7 KB

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