category.vue 4.6 KB

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