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