category.vue 4.7 KB

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