category.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view class="content">
  3. <!-- 兼容小程序搜索 -->
  4. <!-- #ifdef MP -->
  5. <!-- 页面跳转组件 -->
  6. <navigator url="/pages/product/search">
  7. <view class="input-box flex">
  8. <view class=" input-content flex">
  9. <view class="iconfont iconsearch"></view>
  10. <view class="input"><input disabled placeholder="请输入搜索内容" /></view>
  11. </view>
  12. <!-- <view class="input-button flex" ><text>搜索</text></view> -->
  13. </view>
  14. </navigator>
  15. <!-- #endif -->
  16. <!-- #ifdef H5 -->
  17. <!-- <view class="cate-bg"></view> -->
  18. <!-- #endif -->
  19. <div class="classBox">
  20. <scroll-view scroll-y class="left-aside">
  21. <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>
  22. </scroll-view>
  23. <scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
  24. <view v-for="item in flist" :key="item.id" class="s-list" :id="'main-' + item.id">
  25. <text class="s-item">{{ item.cate_name }}</text>
  26. <view class="t-list ">
  27. <view @click="navToList(item.id, titem.id)" class="t-item" v-for="titem in item.children" :key="titem.id">
  28. <image :src="titem.pic" mode="scaleToFill" :lazy-load="true"></image>
  29. <text>{{ titem.cate_name }}</text>
  30. </view>
  31. </view>
  32. </view>
  33. </scroll-view>
  34. </div>
  35. <u-tabbar v-if="mystore" v-model="current" :list="tabbar" active-color="#FF0000" inactive-color="#f19c99"></u-tabbar>
  36. <u-tabbar v-else v-model="current" :list="tabbar1" active-color="#FF0000" inactive-color="#f19c99"></u-tabbar>
  37. </view>
  38. </template>
  39. <script>
  40. import { getList } from '@/api/category.js';
  41. import { tabbar, tabbar1 } from '@/utils/tabbar.js';
  42. export default {
  43. data() {
  44. return {
  45. mystore: '',
  46. sizeCalcState: false,
  47. tabScrollTop: 0,
  48. currentId: 1,
  49. flist: [],
  50. current: 0,
  51. tabbar: tabbar,
  52. tabbar1: tabbar1
  53. };
  54. },
  55. onShow() {
  56. let value = uni.getStorageSync('mystore');
  57. this.mystore = value;
  58. console.log('分类this.mystore', this.mystore);
  59. },
  60. onLoad() {
  61. this.loadData();
  62. },
  63. // 监听导航栏输入框点击事件
  64. onNavigationBarSearchInputClicked(e) {
  65. uni.navigateTo({
  66. url: '/pages/product/search'
  67. });
  68. },
  69. methods: {
  70. // 载入数据
  71. async loadData() {
  72. let obj = this;
  73. getList({})
  74. .then(({ data }) => {
  75. console.log(data);
  76. obj.flist = data;
  77. console.log(obj.flist);
  78. obj.currentId = obj.flist[0].id;
  79. })
  80. .catch(err => {
  81. console.log(err);
  82. });
  83. },
  84. //一级分类点击
  85. tabtap(item) {
  86. console.log(item);
  87. // 判断有没有初始化页面高度对象数据
  88. if (!this.sizeCalcState) {
  89. this.calcSize();
  90. }
  91. // 获取当前点击的id
  92. this.currentId = item.id;
  93. this.tabScrollTop = item.top;
  94. },
  95. //右侧栏滚动
  96. asideScroll(e) {
  97. // 判断有没有初始化页面高度对象数据
  98. if (!this.sizeCalcState) {
  99. this.calcSize();
  100. }
  101. // 获取当前滚轮高度
  102. let scrollTop = e.detail.scrollTop;
  103. let box = 0; //列表包裹框高度初始化
  104. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  105. // 查询当前页面对象
  106. let view = uni.createSelectorQuery().select('.content');
  107. view.fields(
  108. {
  109. id: true,
  110. dataset: true,
  111. rect: true,
  112. size: true,
  113. scrollOffset: true
  114. },
  115. function(e) {
  116. // 保存包裹框高度
  117. box = e.height;
  118. }
  119. ).exec();
  120. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  121. let tabs = this.flist.filter(item => item.top - 10 <= scrollTop).reverse();
  122. if (tabs.length > 0) {
  123. // 判断是否已经到达滚轮底部
  124. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  125. this.currentId = this.flist[this.flist.length - 1].id;
  126. } else {
  127. this.currentId = tabs[0].id;
  128. }
  129. }
  130. },
  131. //计算右侧栏每个tab的高度等信息
  132. calcSize() {
  133. let h = 0;
  134. this.flist.forEach(item => {
  135. let view = uni.createSelectorQuery().select('#main-' + item.id);
  136. view.fields(
  137. {
  138. size: true
  139. },
  140. data => {
  141. item.top = h;
  142. h += data.height;
  143. item.bottom = h;
  144. }
  145. ).exec();
  146. });
  147. this.sizeCalcState = true;
  148. },
  149. navToList(sid, tid) {
  150. // 点击导航跳转到详细页面
  151. uni.navigateTo({
  152. url: '/pages/product/list?fid=' + this.currentId + '&sid=' + sid + '&tid=' + tid
  153. });
  154. }
  155. }
  156. };
  157. </script>
  158. <style lang="scss">
  159. /* #ifdef MP || APP-PLUS*/
  160. .input-box {
  161. padding: 25rpx;
  162. /* #ifdef APP-PLUS */
  163. margin-top: var(--status-bar-height);
  164. /* #endif */
  165. background-color: #ff0000;
  166. height: 44px;
  167. .iconsearch {
  168. font-size: 50rpx;
  169. }
  170. .input-content {
  171. border-radius: 99rpx;
  172. flex-grow: 1;
  173. padding: 5rpx 30rpx;
  174. // background-color: rgba(231, 231, 231, 0.7);
  175. background: #ffffff;
  176. .input {
  177. flex-grow: 1;
  178. input {
  179. font-size: $font-base;
  180. }
  181. }
  182. }
  183. .input-button {
  184. padding-left: 20rpx;
  185. font-size: $font-base;
  186. height: 100%;
  187. }
  188. }
  189. /* #endif */
  190. page,
  191. .content {
  192. height: 100%;
  193. background-color: #f8f8f8;
  194. }
  195. .content {
  196. .cate-bg {
  197. position: fixed;
  198. top: 0;
  199. width: 750rpx;
  200. height: 44px;
  201. z-index: 10;
  202. // background: linear-gradient(90deg, #FF8E00, #FE762F);
  203. background: #ff0000;
  204. }
  205. .classBox {
  206. // padding-top: 44px;
  207. display: flex;
  208. height: calc(100% - 44px);
  209. /* #ifdef APP-PLUS || MP */
  210. height: calc(100% - 88px);
  211. /* #endif */
  212. }
  213. }
  214. .left-aside {
  215. flex-shrink: 0;
  216. width: 200rpx;
  217. height: 100%;
  218. background-color: #fff;
  219. }
  220. .f-item {
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. width: 100%;
  225. height: 100rpx;
  226. font-size: 28rpx;
  227. color: $font-color-base;
  228. position: relative;
  229. &.active {
  230. color: $base-color;
  231. background: #f8f8f8;
  232. &:before {
  233. content: '';
  234. position: absolute;
  235. left: 0;
  236. top: 50%;
  237. transform: translateY(-50%);
  238. height: 36rpx;
  239. width: 8rpx;
  240. background-color: $base-color;
  241. border-radius: 0 4px 4px 0;
  242. opacity: 0.8;
  243. }
  244. }
  245. }
  246. .right-aside {
  247. flex: 1;
  248. overflow: hidden;
  249. padding-left: 20rpx;
  250. padding-right: 20rpx;
  251. }
  252. .s-item {
  253. display: flex;
  254. align-items: center;
  255. height: 70rpx;
  256. padding-top: 8rpx;
  257. font-size: 28rpx;
  258. color: $font-color-dark;
  259. }
  260. .t-list {
  261. display: flex;
  262. flex-wrap: wrap;
  263. border-radius: 15rpx;
  264. width: 100%;
  265. background: #fff;
  266. padding-top: 12rpx;
  267. &:after {
  268. content: '';
  269. flex: 99;
  270. height: 0;
  271. }
  272. }
  273. .t-item {
  274. flex-shrink: 0;
  275. display: flex;
  276. justify-content: center;
  277. align-items: center;
  278. flex-direction: column;
  279. width: 171rpx;
  280. font-size: 26rpx;
  281. color: #666;
  282. padding-bottom: 20rpx;
  283. image {
  284. width: 140rpx;
  285. height: 140rpx;
  286. }
  287. }
  288. </style>