category.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. mer_id:0,//商户id
  29. };
  30. },
  31. onLoad() {
  32. this.loadData();
  33. },
  34. // 监听导航栏输入框点击事件
  35. onNavigationBarSearchInputClicked(e) {
  36. uni.navigateTo({
  37. url: '/pages/product/search'
  38. });
  39. },
  40. methods: {
  41. // 载入数据
  42. async loadData() {
  43. let obj = this;
  44. getCategoryList({})
  45. .then(({ data }) => {
  46. obj.flist = data.map(function(s) {
  47. return s;
  48. });
  49. })
  50. .catch(err => {
  51. console.log(err);
  52. });
  53. },
  54. //一级分类点击
  55. tabtap(item) {
  56. console.log(item);
  57. // 判断有没有初始化页面高度对象数据
  58. if (!this.sizeCalcState) {
  59. this.calcSize();
  60. }
  61. // 获取当前点击的id
  62. this.currentId = item.id;
  63. console.log(item.top);
  64. this.tabScrollTop = item.top;
  65. console.log(this.tabScrollTop);
  66. },
  67. //右侧栏滚动
  68. asideScroll(e) {
  69. // 判断有没有初始化页面高度对象数据
  70. if (!this.sizeCalcState) {
  71. this.calcSize();
  72. }
  73. let scrollTop = e.detail.scrollTop;
  74. let box = 0; //列表包裹框高度初始化
  75. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  76. // 查询当前页面对象
  77. let view = uni.createSelectorQuery().select('.content');
  78. view.fields(
  79. {
  80. id: true,
  81. dataset: true,
  82. rect: true,
  83. size: true,
  84. scrollOffset: true
  85. },
  86. function(e) {
  87. // 保存包裹框高度
  88. box = e.height;
  89. }
  90. ).exec();
  91. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  92. let tabs = this.flist.filter(item =>( item.top-10) <= scrollTop).reverse();
  93. if (tabs.length > 0) {
  94. // 判断是否已经到达滚轮底部
  95. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  96. this.currentId = this.flist[this.flist.length - 1].id;
  97. } else {
  98. this.currentId = tabs[0].id;
  99. }
  100. }
  101. },
  102. //计算右侧栏每个tab的高度等信息
  103. calcSize() {
  104. let h = 0;
  105. this.flist.forEach(item => {
  106. let view = uni.createSelectorQuery().select('#main-' + item.id);
  107. view.fields(
  108. {
  109. size: true
  110. },
  111. data => {
  112. item.top = h;
  113. h += data.height;
  114. item.bottom = h;
  115. }
  116. ).exec();
  117. });
  118. this.sizeCalcState = true;
  119. },
  120. navToList(sid, tid) {
  121. // 点击导航跳转到详细页面
  122. uni.navigateTo({
  123. url: '/pages/product/list?fid='+this.currentId+'&sid='+sid+'&tid='+tid
  124. });
  125. }
  126. }
  127. };
  128. </script>
  129. <style lang="scss">
  130. page,
  131. .content {
  132. height: 100%;
  133. background-color: #f8f8f8;
  134. }
  135. .content {
  136. display: flex;
  137. }
  138. .left-aside {
  139. flex-shrink: 0;
  140. width: 200rpx;
  141. height: 100%;
  142. background-color: #fff;
  143. }
  144. .f-item {
  145. display: flex;
  146. align-items: center;
  147. justify-content: center;
  148. width: 100%;
  149. height: 100rpx;
  150. font-size: 28rpx;
  151. color: $font-color-base;
  152. position: relative;
  153. &.active {
  154. // color: $base-color;
  155. color: #000;
  156. font-weight: bold;
  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>