Carousel.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div class="carousel" :style="{backgroundColor: activeColor}">
  3. <div class="container">
  4. <slot></slot>
  5. <div class="carousel-container">
  6. <div v-swiper:mySwiper="swiperOption">
  7. <div class="swiper-wrapper">
  8. <div class="swiper-slide" v-for="banner in banners.data">
  9. <a :href="banner.hrefUrl" target="_blank" v-if="banner.hrefUrl">
  10. <img :src="banner.pictureUrl"/>
  11. </a>
  12. <span v-if="!banner.hrefUrl">
  13. <img :src="banner.pictureUrl"/>
  14. </span>
  15. </div>
  16. <div class="swiper-button-prev"><i class="iconfont icon-arrow-left"></i></div>
  17. <div class="swiper-button-next"><i class="iconfont icon-arrow-right"></i></div>
  18. </div>
  19. <div class="swiper-pagination swiper-pagination-bullets"></div>
  20. </div>
  21. </div>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'carousel',
  28. data () {
  29. return {
  30. activeSlide: 0,
  31. swiperOption: {
  32. autoplay: 6000,
  33. pagination: '.swiper-pagination',
  34. paginationClickable: true,
  35. mousewheelControl: false,
  36. effect: 'fade',
  37. lazyLoading: true,
  38. loop: true,
  39. prevButton: '.swiper-button-prev',
  40. nextButton: '.swiper-button-next',
  41. onTransitionStart: (swiper) => {
  42. // 不要通过vue刷新dom,会导致pagination无法刷新
  43. // this.activeSlide = swiper.activeIndex
  44. if (this.banners.data.length && swiper.activeIndex > this.banners.data.length) {
  45. swiper.activeIndex = 1
  46. }
  47. if (this.banners.data.length && swiper.activeIndex <= 0) {
  48. swiper.activeIndex = this.banners.data.length
  49. }
  50. let carousel = document.querySelector('.carousel')
  51. if (carousel && carousel !== null) {
  52. carousel.style.backgroundColor =
  53. this.banners.data[swiper.activeIndex - 1].metadata['background-color']
  54. }
  55. }
  56. }
  57. }
  58. },
  59. computed: {
  60. banners () {
  61. return this.$store.state.carousel.banners
  62. },
  63. activeColor () {
  64. return this.banners.data.length ? this.banners.data[this.activeSlide].metadata['background-color'] : null
  65. }
  66. }
  67. }
  68. </script>
  69. <style lang="scss" scoped>
  70. @import '~assets/scss/variables';
  71. $carousel_width: 990px;
  72. $carousel_height: 477px;
  73. .carousel {
  74. transition: background-color .3s;
  75. position: relative;
  76. margin-bottom: $lg-pad;
  77. .carousel-container {
  78. width: $carousel_width;
  79. height: $carousel_height;
  80. margin-left: 200px;
  81. overflow: hidden;
  82. .swiper-wrapper {
  83. .swiper-slide {
  84. img {
  85. display: block;
  86. height: $carousel_height;
  87. }
  88. a[href='']:hover{
  89. cursor: default;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. </style>