ui-swiper.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div v-bind="$attrs" class="relative w-full">
  3. <swiper ref="swiper" class="relative w-full h-full" :options="swiperOption">
  4. <swiper-slide v-for="(item, index) in items" :key="index">
  5. <div class="w-full h-full absolute bg-[rgba(0,0,0,0.3)]"></div>
  6. <img class="w-full h-full object-cover" :src="item.url" />
  7. <div
  8. :class="`absolute bottom-23px left-26px font-normal z-50 flex text-white text-size-20px ${
  9. titleClass ?? ''
  10. }`"
  11. >
  12. {{ item.title }}
  13. </div>
  14. </swiper-slide>
  15. </swiper>
  16. <div v-if="showPagination" :class="`absolute bottom-22px right-12px z-50 flex ${paginationclass}`">
  17. <div
  18. @click="clickPagination(index)"
  19. v-for="(item, index) in items"
  20. :key="index"
  21. :class="`dot ${index === activeIndex ? 'selected' : ''}`"
  22. ></div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import { nanoid } from "nanoid";
  28. import { Swiper, SwiperSlide } from "vue-awesome-swiper";
  29. import "swiper/swiper-bundle.min.css";
  30. import SwiperCore, { Autoplay } from "swiper/core";
  31. SwiperCore.use([Autoplay]);
  32. export default {
  33. components: {
  34. Swiper,
  35. SwiperSlide,
  36. },
  37. props: {
  38. items: {
  39. default: [],
  40. },
  41. showPagination: {
  42. default: false,
  43. },
  44. paginationclass:{
  45. default: ''
  46. } ,
  47. titleClass: {},
  48. },
  49. computed: {
  50. paginationId: () => nanoid(6),
  51. swiperOption: () => ({
  52. slidesPerView: 1,
  53. click: false,
  54. // loop: true,
  55. autoplay: {
  56. delay: 2000,
  57. // pauseOnMouseEnter: true,
  58. },
  59. }),
  60. },
  61. data() {
  62. return {
  63. activeIndex: 0,
  64. };
  65. },
  66. methods: {
  67. change(swiper) {
  68. if (!this.showPagination) return;
  69. this.activeIndex = swiper.activeIndex;
  70. },
  71. clickPagination(index) {
  72. this.$refs.swiper.$swiper.slideTo(index);
  73. },
  74. },
  75. mounted() {
  76. this.$refs.swiper.$swiper.on("activeIndexChange", this.change);
  77. },
  78. };
  79. </script>
  80. <style>
  81. .swiper-pagination-bullet-active {
  82. @apply bg-white;
  83. }
  84. .swiper-pagination-bullet {
  85. @apply bg-rgba(255,255,255,1);
  86. }
  87. .dot {
  88. @apply w-12px h-12px rounded-full bg-[rgba(255,255,255,0.3)] mr-10px cursor-pointer;
  89. }
  90. .dot.selected {
  91. @apply bg-white;
  92. }
  93. </style>