category.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view class="content">
  3. <scroll-view scroll-y class="left-aside">
  4. <view v-for="item in flist" :key="item.id" class="f-item b-b" :class="{ active: item.id === currentId }" @click="tabtap(item)">{{ item.cate_name }}</view>
  5. </scroll-view>
  6. <scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
  7. <view v-for="item in flist" :key="item.id" class="s-list" :id="'main-' + item.id">
  8. <text class="s-item">{{ item.cate_name }}</text>
  9. <view class="t-list ">
  10. <view @click="navToList(item.id, titem.id)" class="t-item" v-for="titem in item.children" :key="titem.id">
  11. <image :src="titem.pic"></image>
  12. <text>{{ titem.cate_name }}</text>
  13. </view>
  14. </view>
  15. </view>
  16. </scroll-view>
  17. <!-- <u-tabbar v-model="current" :list="tabbar1" active-color="#FF0000" inactive-color="#f19c99"></u-tabbar> -->
  18. </view>
  19. </template>
  20. <script>
  21. import { getCategoryList } from '@/api/product.js';
  22. import {
  23. tabbar,tabbar1
  24. } from "@/utils/tabbar.js";
  25. export default {
  26. data() {
  27. return {
  28. tabbar: tabbar,
  29. tabbar1: tabbar1,
  30. sizeCalcState: false,
  31. tabScrollTop: 0,
  32. currentId: 9,
  33. flist: [],
  34. current: 4
  35. };
  36. },
  37. onLoad() {
  38. this.loadData();
  39. },
  40. // 监听导航栏输入框点击事件
  41. onNavigationBarSearchInputClicked(e) {
  42. uni.navigateTo({
  43. url: '/pages/product/search'
  44. });
  45. },
  46. methods: {
  47. // 载入数据
  48. async loadData() {
  49. let obj = this;
  50. getCategoryList({})
  51. .then(({ data }) => {
  52. obj.flist = data.map(function(s) {
  53. return s;
  54. });
  55. })
  56. .catch(err => {
  57. console.log(err);
  58. });
  59. },
  60. //一级分类点击
  61. tabtap(item) {
  62. console.log(item);
  63. // 判断有没有初始化页面高度对象数据
  64. if (!this.sizeCalcState) {
  65. this.calcSize();
  66. }
  67. // 获取当前点击的id
  68. this.currentId = item.id;
  69. console.log(item.top);
  70. this.tabScrollTop = item.top;
  71. console.log(this.tabScrollTop);
  72. },
  73. //右侧栏滚动
  74. asideScroll(e) {
  75. // 判断有没有初始化页面高度对象数据
  76. if (!this.sizeCalcState) {
  77. this.calcSize();
  78. }
  79. let scrollTop = e.detail.scrollTop;
  80. let box = 0; //列表包裹框高度初始化
  81. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  82. // 查询当前页面对象
  83. let view = uni.createSelectorQuery().select('.content');
  84. view.fields(
  85. {
  86. id: true,
  87. dataset: true,
  88. rect: true,
  89. size: true,
  90. scrollOffset: true
  91. },
  92. function(e) {
  93. // 保存包裹框高度
  94. box = e.height;
  95. }
  96. ).exec();
  97. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  98. let tabs = this.flist.filter(item =>( item.top-10) <= scrollTop).reverse();
  99. if (tabs.length > 0) {
  100. // 判断是否已经到达滚轮底部
  101. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  102. this.currentId = this.flist[this.flist.length - 1].id;
  103. } else {
  104. this.currentId = tabs[0].id;
  105. }
  106. }
  107. },
  108. //计算右侧栏每个tab的高度等信息
  109. calcSize() {
  110. let h = 0;
  111. this.flist.forEach(item => {
  112. let view = uni.createSelectorQuery().select('#main-' + item.id);
  113. view.fields(
  114. {
  115. size: true
  116. },
  117. data => {
  118. item.top = h;
  119. h += data.height;
  120. item.bottom = h;
  121. }
  122. ).exec();
  123. });
  124. this.sizeCalcState = true;
  125. },
  126. navToList(sid, tid) {
  127. // 点击导航跳转到详细页面
  128. uni.navigateTo({
  129. url: '/pages/product/list?fid='+this.currentId+'&sid='+sid+'&tid='+tid
  130. });
  131. }
  132. }
  133. };
  134. </script>
  135. <style lang="scss">
  136. page,
  137. .content {
  138. height: 100%;
  139. background-color: #f8f8f8;
  140. }
  141. .content {
  142. display: flex;
  143. }
  144. .left-aside {
  145. flex-shrink: 0;
  146. width: 200rpx;
  147. height: 100%;
  148. background-color: #fff;
  149. }
  150. .f-item {
  151. display: flex;
  152. align-items: center;
  153. justify-content: center;
  154. width: 100%;
  155. height: 100rpx;
  156. font-size: 28rpx;
  157. color: $font-color-base;
  158. position: relative;
  159. &.active {
  160. color: $base-color;
  161. background: #f8f8f8;
  162. &:before {
  163. content: '';
  164. position: absolute;
  165. left: 0;
  166. top: 50%;
  167. transform: translateY(-50%);
  168. height: 36rpx;
  169. width: 8rpx;
  170. background-color: $base-color;
  171. border-radius: 0 4px 4px 0;
  172. opacity: 0.8;
  173. }
  174. }
  175. }
  176. .right-aside {
  177. flex: 1;
  178. overflow: hidden;
  179. padding-left: 20rpx;
  180. padding-right: 20rpx;
  181. }
  182. .s-item {
  183. display: flex;
  184. align-items: center;
  185. height: 70rpx;
  186. padding-top: 8rpx;
  187. font-size: 28rpx;
  188. color: $font-color-dark;
  189. }
  190. .t-list {
  191. display: flex;
  192. flex-wrap: wrap;
  193. border-radius: 15rpx;
  194. width: 100%;
  195. background: #fff;
  196. padding-top: 12rpx;
  197. &:after {
  198. content: '';
  199. flex: 99;
  200. height: 0;
  201. }
  202. }
  203. .t-item {
  204. flex-shrink: 0;
  205. display: flex;
  206. justify-content: center;
  207. align-items: center;
  208. flex-direction: column;
  209. width: 171rpx;
  210. font-size: 26rpx;
  211. color: #666;
  212. padding-bottom: 20rpx;
  213. image {
  214. width: 140rpx;
  215. height: 140rpx;
  216. }
  217. }
  218. </style>