index.vue 7.5 KB

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