category.vue 4.7 KB

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