category.vue 5.0 KB

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