index2.vue 7.1 KB

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