swiper-evo.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <script setup lang="ts" generic="T extends Object">
  2. const props = withDefaults(defineProps<{ items?: T[]; dotColor?: string }>(), {
  3. items: () => [],
  4. dotColor: () => '#000',
  5. })
  6. const modelValue = defineModel({ type: Number, default: 0 })
  7. const handleSwiperChange = async ({ detail: { current } }) => {
  8. modelValue.value = current
  9. }
  10. </script>
  11. <template>
  12. <div class="w-full h-full relative">
  13. <swiper class="w-full h-full" autoplay :current="modelValue" @change="handleSwiperChange">
  14. <template v-for="(it, i) in items" :key="i">
  15. <swiper-item>
  16. <div class="w-full h-full swiper-item"><slot :item="it"></slot></div>
  17. </swiper-item>
  18. </template>
  19. </swiper>
  20. <div class="absolute flex gap-1 dots">
  21. <template v-for="(it, i) in items" :key="i">
  22. <div
  23. class="w-1 h-1 rounded-full"
  24. :class="`${modelValue === i ? `bg-active` : `opacity-50`}`"
  25. :style="{ background: dotColor }"
  26. ></div>
  27. </template>
  28. </div>
  29. </div>
  30. </template>
  31. <style scoped>
  32. .dots {
  33. bottom: 10px;
  34. left: 50%;
  35. transform: translateX(-50%);
  36. z-index: 1000;
  37. }
  38. .bg-active {
  39. width: 30rpx;
  40. transition: width 0.3s ease-in-out;
  41. }
  42. </style>