category.vue 5.0 KB

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