ClassifyScroll.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <Swiper
  3. class="classify-scroll"
  4. :modules="[FreeMode]"
  5. free-mode
  6. slides-per-view="auto"
  7. :space-between="10"
  8. >
  9. <SwiperSlide
  10. v-for="(item, index) in activeTab === 0 ? storageList : exhibitList"
  11. :key="item.id"
  12. >
  13. <div
  14. class="classify-scroll-item"
  15. :class="{
  16. active: (isStorage ? storageActive : exhibitActive) === index,
  17. }"
  18. @click="handleClick(item, index)"
  19. >
  20. {{ item.name }}
  21. </div>
  22. </SwiperSlide>
  23. </Swiper>
  24. </template>
  25. <script setup>
  26. import { ref, computed, watch } from "vue";
  27. import { FreeMode } from "swiper/modules";
  28. import { Swiper, SwiperSlide } from "swiper/vue";
  29. import { getStorageTreeApi, getExhibitTypeListApi } from "@/api";
  30. const props = defineProps({
  31. activeTab: {
  32. type: Number,
  33. required: true,
  34. },
  35. storageInitVal: {
  36. type: Number,
  37. default: 0,
  38. },
  39. exhibitInitVal: {
  40. type: Number,
  41. default: 0,
  42. },
  43. });
  44. const isStorage = computed(() => props.activeTab === 0);
  45. const emits = defineEmits(["storageId", "exhibitId", "init"]);
  46. const storageActive = ref(0);
  47. const exhibitActive = ref(0);
  48. const storageList = ref([]);
  49. const exhibitList = ref([]);
  50. const getStorageTree = async () => {
  51. const data = await getStorageTreeApi();
  52. storageList.value = data;
  53. if (props.storageInitVal) {
  54. storageActive.value = data.findIndex((i) => i.id === props.storageInitVal);
  55. }
  56. };
  57. const getExhibitTypeList = async () => {
  58. const data = await getExhibitTypeListApi();
  59. exhibitList.value = data;
  60. if (props.exhibitInitVal) {
  61. exhibitActive.value = data.findIndex((i) => i.id === props.exhibitInitVal);
  62. }
  63. };
  64. const handleClick = (item, index) => {
  65. if (isStorage.value) {
  66. storageActive.value = index;
  67. emits("storageId", item.id);
  68. } else {
  69. exhibitActive.value = index;
  70. emits("exhibitId", item.id);
  71. }
  72. };
  73. watch(
  74. () => props.activeTab,
  75. async () => {
  76. if (isStorage.value && !storageList.value.length) {
  77. await getStorageTree();
  78. emits("init", storageList.value[0].id);
  79. } else if (!isStorage.value && !exhibitList.value.length) {
  80. await getExhibitTypeList();
  81. emits("init", exhibitList.value[0].id);
  82. }
  83. },
  84. {
  85. immediate: true,
  86. }
  87. );
  88. </script>
  89. <style scoped lang="scss">
  90. .classify-scroll {
  91. padding: 35px 0 25px;
  92. /* prettier-ignore */
  93. height: 60PX;
  94. background: #fcfcfc;
  95. box-sizing: border-box;
  96. z-index: 10;
  97. .swiper-slide {
  98. width: auto;
  99. &:first-child {
  100. margin-left: 48px;
  101. }
  102. &:last-child {
  103. margin-right: 48px;
  104. }
  105. }
  106. &-item {
  107. display: inline-block;
  108. padding: 10px 35px;
  109. font-size: 27px;
  110. white-space: nowrap;
  111. color: var(--van-primary-color);
  112. border: 2px solid var(--van-primary-color);
  113. border-radius: 50px;
  114. &.active {
  115. color: var(--van-white);
  116. background: var(--van-primary-color);
  117. }
  118. }
  119. }
  120. </style>