category.vue 5.0 KB

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