Carousel.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. document.querySelector('.carousel').style.backgroundColor =
  51. this.banners.data[swiper.activeIndex - 1].metadata['background-color']
  52. }
  53. }
  54. }
  55. },
  56. computed: {
  57. banners () {
  58. let banner = this.$store.state.carousel.banners
  59. banner.data.sort(function (a, b) {
  60. return a.orderNumber - b.orderNumber
  61. })
  62. return banner
  63. },
  64. activeColor () {
  65. return this.banners.data.length ? this.banners.data[this.activeSlide].metadata['background-color'] : null
  66. }
  67. }
  68. }
  69. </script>
  70. <style lang="scss" scoped>
  71. @import '~assets/scss/variables';
  72. $carousel_width: 990px;
  73. $carousel_height: 477px;
  74. .carousel {
  75. transition: background-color .3s;
  76. position: relative;
  77. margin-bottom: $lg-pad;
  78. .carousel-container {
  79. width: $carousel_width;
  80. height: $carousel_height;
  81. margin-left: 200px;
  82. overflow: hidden;
  83. .swiper-wrapper {
  84. .swiper-slide {
  85. img {
  86. display: block;
  87. height: $carousel_height;
  88. }
  89. a[href='']:hover{
  90. cursor: default;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. </style>