Carousel.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // 解决点击分页器后图片就不能轮播的问题
  35. autoplayDisableOnInteraction: false,
  36. paginationClickable: true,
  37. mousewheelControl: false,
  38. effect: 'fade',
  39. lazyLoading: true,
  40. loop: true,
  41. prevButton: '.swiper-button-prev',
  42. nextButton: '.swiper-button-next',
  43. onTransitionStart: (swiper) => {
  44. // 不要通过vue刷新dom,会导致pagination无法刷新
  45. // this.activeSlide = swiper.activeIndex
  46. if (this.banners.data.length && swiper.activeIndex > this.banners.data.length) {
  47. swiper.activeIndex = 1
  48. }
  49. if (this.banners.data.length && swiper.activeIndex <= 0) {
  50. swiper.activeIndex = this.banners.data.length
  51. }
  52. document.querySelector('.carousel').style.backgroundColor =
  53. this.banners.data[swiper.activeIndex - 1].metadata['background-color']
  54. }
  55. }
  56. }
  57. },
  58. computed: {
  59. banners () {
  60. let banner = this.$store.state.carousel.banners
  61. banner.data.sort(function (a, b) {
  62. return a.orderNumber - b.orderNumber
  63. })
  64. return banner
  65. },
  66. activeColor () {
  67. return this.banners.data.length ? this.banners.data[this.activeSlide].metadata['background-color'] : null
  68. }
  69. }
  70. }
  71. </script>
  72. <style lang="scss" scoped>
  73. @import '~assets/scss/variables';
  74. $carousel_width: 990px;
  75. $carousel_height: 477px;
  76. .carousel {
  77. transition: background-color .3s;
  78. position: relative;
  79. margin-bottom: $lg-pad;
  80. .carousel-container {
  81. width: $carousel_width;
  82. height: $carousel_height;
  83. margin-left: 200px;
  84. overflow: hidden;
  85. .swiper-wrapper {
  86. .swiper-slide {
  87. img {
  88. display: block;
  89. height: $carousel_height;
  90. }
  91. a[href='']:hover{
  92. cursor: default;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. </style>