category.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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="#333333" inactive-color='#999999' 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: auto;
  136. min-height: 100%;
  137. background-color: #f8f8f8;
  138. }
  139. .content {
  140. display: flex;
  141. }
  142. .left-aside {
  143. flex-shrink: 0;
  144. width: 200rpx;
  145. height: 100%;
  146. background-color: #fff;
  147. }
  148. .f-item {
  149. display: flex;
  150. align-items: center;
  151. justify-content: center;
  152. width: 100%;
  153. height: 100rpx;
  154. font-size: 28rpx;
  155. color: $font-color-base;
  156. position: relative;
  157. &.active {
  158. color: $base-color;
  159. background: #f8f8f8;
  160. &:before {
  161. content: '';
  162. position: absolute;
  163. left: 0;
  164. top: 50%;
  165. transform: translateY(-50%);
  166. height: 36rpx;
  167. width: 8rpx;
  168. background-color: $base-color;
  169. border-radius: 0 4px 4px 0;
  170. opacity: 0.8;
  171. }
  172. }
  173. }
  174. .right-aside {
  175. flex: 1;
  176. overflow: hidden;
  177. padding-left: 20rpx;
  178. padding-right: 20rpx;
  179. padding-bottom: 44px;
  180. }
  181. .s-item {
  182. display: flex;
  183. align-items: center;
  184. height: 70rpx;
  185. padding-top: 8rpx;
  186. font-size: 28rpx;
  187. color: $font-color-dark;
  188. }
  189. .t-list {
  190. display: flex;
  191. flex-wrap: wrap;
  192. border-radius: 15rpx;
  193. width: 100%;
  194. background: #fff;
  195. padding-top: 12rpx;
  196. &:after {
  197. content: '';
  198. flex: 99;
  199. height: 0;
  200. }
  201. }
  202. .t-item {
  203. flex-shrink: 0;
  204. display: flex;
  205. justify-content: center;
  206. align-items: center;
  207. flex-direction: column;
  208. width: 171rpx;
  209. font-size: 26rpx;
  210. color: #666;
  211. padding-bottom: 20rpx;
  212. image {
  213. width: 140rpx;
  214. height: 140rpx;
  215. }
  216. }
  217. </style>