category.vue 5.6 KB

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