category.vue 4.6 KB

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