Carousel.vue 3.4 KB

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