list.vue 8.0 KB

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