Carousel.vue 2.0 KB

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