category.vue 4.7 KB

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