category.vue 4.9 KB

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