Carousel.vue 3.3 KB

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