Carousel.vue 3.5 KB

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