cate.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view>
  3. <view class="scroll-view-container">
  4. <!-- 左侧的滚动视图区域 -->
  5. <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
  6. <block v-for="(item, i) in cateList" :key="i">
  7. <view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChanged(i)">
  8. {{item.cat_name}}</view>
  9. </block>
  10. </scroll-view>
  11. <!-- 右侧的滚动视图区域 -->
  12. <scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
  13. <view class="cate-lv2" v-for="(item2, i2) in cateLevel2" :key="i2">
  14. <view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
  15. <!-- 动态渲染三级分类的列表数据 -->
  16. <view class="cate-lv3-list">
  17. <!-- 三级分类 Item 项 -->
  18. <view class="cate-lv3-item" v-for="(item3, i3) in item2.children"
  19. :key="i3" @click="gotoGoodsList(item3)">
  20. <!-- 图片 -->
  21. <image :src="item3.cat_icon"></image>
  22. <!-- 文本 -->
  23. <text>{{item3.cat_name}}</text>
  24. </view>
  25. </view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
  36. wh: 0,
  37. // 分类数据列表
  38. cateList: [],
  39. // 当前选中项的索引,默认让第一项被选中
  40. active: 0,
  41. // 二级分类列表
  42. cateLevel2: [],
  43. // 滚动条距离顶部的距离
  44. scrollTop: 0
  45. };
  46. },
  47. onLoad() {
  48. // 获取当前系统的信息
  49. const sysInfo = uni.getSystemInfoSync()
  50. // 为 wh 窗口可用高度动态赋值
  51. this.wh = sysInfo.windowHeight,
  52. // 调用获取分类列表数据的方法
  53. this.getCateList()
  54. },
  55. methods: {
  56. async getCateList() {
  57. // 发起请求
  58. const { data: res } = await uni.$http.get('/api/public/v1/categories')
  59. // 判断是否获取失败
  60. if (res.meta.status !== 200) return uni.$showMsg()
  61. // 转存数据
  62. this.cateList = res.message
  63. // 为二级分类赋值
  64. this.cateLevel2 = res.message[0].children
  65. },
  66. // 选中项改变的事件处理函数
  67. activeChanged(i) {
  68. this.active = i
  69. // 为二级分类列表重新赋值
  70. this.cateLevel2 = this.cateList[i].children
  71. // 让 scrollTop 的值在 0 与 1 之间切换
  72. this.scrollTop = this.scrollTop === 0 ? 1 : 0
  73. },
  74. // 点击三级分类项跳转到商品列表页面
  75. gotoGoodsList(item3) {
  76. uni.navigateTo({
  77. url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
  78. })
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss">
  84. .scroll-view-container {
  85. display: flex;
  86. .left-scroll-view {
  87. width: 120px;
  88. .left-scroll-view-item {
  89. line-height: 60px;
  90. background-color: #f7f7f7;
  91. text-align: center;
  92. font-size: 12px;
  93. // 激活项的样式
  94. &.active {
  95. background-color: #ffffff;
  96. position: relative;
  97. // 渲染激活项左侧的红色指示边线
  98. &::before {
  99. content: ' ';
  100. display: block;
  101. width: 3px;
  102. height: 30px;
  103. background-color: #c00000;
  104. position: absolute;
  105. left: 0;
  106. top: 50%;
  107. transform: translateY(-50%);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. .cate-lv2-title {
  114. font-size: 12px;
  115. font-weight: bold;
  116. text-align: center;
  117. padding: 15px 0;
  118. }
  119. .cate-lv3-list {
  120. display: flex;
  121. flex-wrap: wrap;
  122. .cate-lv3-item {
  123. width: 33.33%;
  124. margin-bottom: 10px;
  125. display: flex;
  126. flex-direction: column;
  127. align-items: center;
  128. image {
  129. width: 60px;
  130. height: 60px;
  131. }
  132. text {
  133. font-size: 12px;
  134. }
  135. }
  136. }
  137. </style>