Swiper.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <script setup lang="ts">
  2. // 引入swiper组件
  3. import { Swiper, SwiperSlide } from 'swiper/vue';
  4. // 引入swiper样式(按需导入)
  5. import 'swiper/css';
  6. import 'swiper/css/pagination'; // 轮播图底面的小圆点
  7. import 'swiper/css/navigation'; // 轮播图两边的左右箭头
  8. import 'swiper/css/scrollbar'; // 轮播图的滚动条
  9. // 引入swiper核心和所需模块
  10. import { Autoplay, Pagination, Navigation, Scrollbar } from 'swiper';
  11. type ImageItem = { image: string; date?: string };
  12. const modules = [Autoplay, Pagination, Navigation, Scrollbar];
  13. const props = defineProps<{ images: ImageItem[] }>();
  14. const slidesPerView = computed(() => {
  15. const count = Array.isArray(props.images) ? props.images.length : 0;
  16. return Math.min(count || 1, 3);
  17. });
  18. </script>
  19. <template>
  20. <div class="home">
  21. <swiper
  22. :modules="modules"
  23. :loop="true"
  24. :slides-per-view="slidesPerView"
  25. :autoplay="{ delay: 4000, disableOnInteraction: false }"
  26. navigation
  27. :pagination="{ clickable: true }"
  28. :scrollbar="{ draggable: true }"
  29. >
  30. <!-- loop可循环轮播,autoplay可自动播放 -->
  31. <swiper-slide v-for="img in props.images" :key="img.date || img.image">
  32. <div>
  33. <span>{{ img?.date }}</span>
  34. <div>
  35. <a-image :src="img?.image" style="width: 200px; height: 290px" />
  36. </div>
  37. </div>
  38. </swiper-slide>
  39. </swiper>
  40. </div>
  41. </template>
  42. <style scoped lang="scss">
  43. .home {
  44. .swiper-slide {
  45. //background-color: #bfc;
  46. height: 200px;
  47. }
  48. .swiper-button-prev,
  49. .swiper-button-next {
  50. transform: translateY(-50%);
  51. }
  52. }
  53. .pc-banner {
  54. position: relative;
  55. .swiper-container {
  56. width: 90%;
  57. }
  58. .button {
  59. width: 100%;
  60. display: flex;
  61. justify-content: space-between;
  62. position: absolute;
  63. top: 50%;
  64. .swiper-button-next {
  65. color: black;
  66. }
  67. }
  68. }
  69. @media only screen and (min-width: 1200px) {
  70. .main {
  71. //background: springgreen;
  72. }
  73. }
  74. /*最大的宽度是1200px,小于1200px都适用于这里*/
  75. @media only screen and (max-width: 960px) {
  76. .main {
  77. width: 100%;
  78. }
  79. }
  80. /*
  81. 大于min-width的宽度,小于max-width宽度
  82. * */
  83. @media only screen and (min-width: 960px) and (max-width: 1200px) {
  84. .main {
  85. width: 100%;
  86. }
  87. }
  88. .swiper-slide {
  89. -webkit-transition: transform 1s;
  90. -moz-transition: transform 1s;
  91. -ms-transition: transform 1s;
  92. -o-transition: transform 1s;
  93. // -webkit-transform: scale(0.7);
  94. // transform: scale(0.7);
  95. }
  96. .swiper-slide-active {
  97. -webkit-transform: scale(1);
  98. transform: scale(1);
  99. }
  100. .none-effect {
  101. -webkit-transition: none;
  102. -moz-transition: none;
  103. -ms-transition: none;
  104. -o-transition: none;
  105. }
  106. .swiper-slide {
  107. img {
  108. width: 100%;
  109. display: block;
  110. }
  111. }
  112. .button {
  113. width: 100%;
  114. margin: auto;
  115. position: relative;
  116. top: 25%;
  117. }
  118. @media screen and (max-width: 750px) {
  119. .button {
  120. width: 100%;
  121. }
  122. }
  123. .swiper-button-prev {
  124. width: 40px;
  125. height: 40px;
  126. border-radius: 50%;
  127. background: url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%2027%2044'%3E%3Cpath%20d%3D'M0%2C22L22%2C0l4.2%2C4.2L8.4%2C22l17.8%2C17.8L22%2C44L0%2C22z'%20fill%3D'%23ffffff'%2F%3E%3C%2Fsvg%3E")
  128. #c9c9ca center 50%/50% 50% no-repeat;
  129. }
  130. .swiper-button-next {
  131. width: 40px;
  132. height: 40px;
  133. border-radius: 50%;
  134. background: url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%2027%2044'%3E%3Cpath%20d%3D'M27%2C22L5%2C44l-4.2-4.2L18.6%2C22L0.8%2C4.2L5%2C0z'%20fill%3D'%23ffffff'%2F%3E%3C%2Fsvg%3E")
  135. #c9c9ca center 50%/50% 50% no-repeat;
  136. }
  137. @media screen and (max-width: 750px) {
  138. .button div {
  139. width: 28px;
  140. height: 28px;
  141. }
  142. }
  143. </style>