category.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.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 slist" :key="item.id" class="s-list" :id="'main-'+item.id">
  10. <text class="s-item">{{item.name}}</text>
  11. <view class="t-list">
  12. <view @click="navToList(item.id, titem.id)" v-if="titem.pid === item.id" class="t-item" v-for="titem in tlist" :key="titem.id">
  13. <image :src="titem.picture"></image>
  14. <text>{{titem.name}}</text>
  15. </view>
  16. </view>
  17. </view>
  18. </scroll-view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. primary:this.$theme.primary,
  26. sizeCalcState: false,
  27. tabScrollTop: 0,
  28. currentId: 1,
  29. flist: [],
  30. slist: [],
  31. tlist: [],
  32. settingFile:getApp().globalData.siteinfo
  33. }
  34. },
  35. onLoad(){
  36. this.loadData();
  37. },
  38. methods: {
  39. async loadData(){
  40. let list = await this.$api.json('cateList');
  41. list.forEach(item=>{
  42. if(!item.pid){
  43. this.flist.push(item); //pid为父级id, 没有pid或者pid=0是一级分类
  44. }else if(!item.picture){
  45. this.slist.push(item); //没有图的是2级分类
  46. }else{
  47. this.tlist.push(item); //3级分类
  48. }
  49. })
  50. },
  51. //一级分类点击
  52. tabtap(item){
  53. if(!this.sizeCalcState){
  54. this.calcSize();
  55. }
  56. this.currentId = item.id;
  57. let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
  58. this.tabScrollTop = this.slist[index].top;
  59. },
  60. //右侧栏滚动
  61. asideScroll(e){
  62. if(!this.sizeCalcState){
  63. this.calcSize();
  64. }
  65. let scrollTop = e.detail.scrollTop;
  66. let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
  67. if(tabs.length > 0){
  68. this.currentId = tabs[0].pid;
  69. }
  70. },
  71. //计算右侧栏每个tab的高度等信息
  72. calcSize(){
  73. let h = 0;
  74. this.slist.forEach(item=>{
  75. let view = uni.createSelectorQuery().select("#main-" + item.id);
  76. view.fields({
  77. size: true
  78. }, data => {
  79. item.top = h;
  80. h += data.height;
  81. item.bottom = h;
  82. }).exec();
  83. })
  84. this.sizeCalcState = true;
  85. },
  86. navToList(sid, tid){
  87. uni.navigateTo({
  88. url: `/pagesD/pages/product/list?fid=${this.currentId}&sid=${sid}&tid=${tid}`
  89. })
  90. }
  91. }
  92. }
  93. </script>
  94. <style lang='scss'>
  95. page,
  96. .content {
  97. /* height: 100%; */
  98. background-color: #f8f8f8;
  99. }
  100. .content {
  101. display: flex;
  102. }
  103. .left-aside {
  104. flex-shrink: 0;
  105. width: 200upx;
  106. /* height: 100%; */
  107. background-color: #fff;
  108. }
  109. .f-item {
  110. display: flex;
  111. align-items: center;
  112. justify-content: center;
  113. width: 100%;
  114. height: 100upx;
  115. font-size: 28upx;
  116. color: $font-color-base;
  117. position: relative;
  118. &.active{
  119. color: $base-color;
  120. background: #f8f8f8;
  121. &:before{
  122. content: '';
  123. position: absolute;
  124. left: 0;
  125. top: 50%;
  126. transform: translateY(-50%);
  127. height: 36upx;
  128. width: 8upx;
  129. background-color: $base-color;
  130. border-radius: 0 4px 4px 0;
  131. opacity: .8;
  132. }
  133. }
  134. }
  135. .right-aside{
  136. flex: 1;
  137. overflow: hidden;
  138. padding-left: 20upx;
  139. }
  140. .s-item{
  141. display: flex;
  142. align-items: center;
  143. height: 70upx;
  144. padding-top: 8upx;
  145. font-size: 28upx;
  146. color: $font-color-dark;
  147. }
  148. .t-list{
  149. display: flex;
  150. flex-wrap: wrap;
  151. width: 100%;
  152. background: #fff;
  153. padding-top: 12upx;
  154. &:after{
  155. content: '';
  156. flex: 99;
  157. height: 0;
  158. }
  159. }
  160. .t-item{
  161. flex-shrink: 0;
  162. display: flex;
  163. justify-content: center;
  164. align-items: center;
  165. flex-direction: column;
  166. width: 176upx;
  167. font-size: 26upx;
  168. color: #666;
  169. padding-bottom: 20upx;
  170. image{
  171. width: 140upx;
  172. height: 140upx;
  173. }
  174. }
  175. </style>