Carousel.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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">
  10. <img :src="banner.pictureUrl"/>
  11. </a>
  12. </div>
  13. </div>
  14. <div class="swiper-pagination swiper-pagination-bullets"></div>
  15. <div class="swiper-button-prev"><i class="iconfont icon-arrow-left"></i></div>
  16. <div class="swiper-button-next"><i class="iconfont icon-arrow-right"></i></div>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'carousel',
  25. data () {
  26. return {
  27. activeSlide: 0,
  28. swiperOption: {
  29. autoplay: 6000,
  30. pagination: '.swiper-pagination',
  31. paginationClickable: true,
  32. mousewheelControl: true,
  33. effect: 'fade',
  34. lazyLoading: true,
  35. prevButton: '.swiper-button-prev',
  36. nextButton: '.swiper-button-next',
  37. onTransitionStart: (swiper) => {
  38. // 不要通过vue刷新dom,会导致pagination无法刷新
  39. // this.activeSlide = swiper.activeIndex
  40. document.querySelector('.carousel').style.backgroundColor =
  41. this.banners.data[swiper.activeIndex].metadata['background-color']
  42. }
  43. }
  44. }
  45. },
  46. computed: {
  47. banners () {
  48. return this.$store.state.carousel.banners
  49. },
  50. activeColor () {
  51. return this.banners.data.length ? this.banners.data[this.activeSlide].metadata['background-color'] : null
  52. }
  53. }
  54. }
  55. </script>
  56. <style lang="scss" scoped>
  57. @import '~assets/scss/variables';
  58. $carousel_width: 990px;
  59. $carousel_height: 477px;
  60. .carousel {
  61. transition: background-color .3s;
  62. position: relative;
  63. margin-bottom: $lg-pad;
  64. .carousel-container {
  65. width: $carousel_width;
  66. height: $carousel_height;
  67. margin-left: 200px;
  68. overflow: hidden;
  69. .swiper-wrapper {
  70. .swiper-slide {
  71. img {
  72. display: block;
  73. height: $carousel_height;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. </style>