Carousel.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: false,
  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. banners: {}
  45. }
  46. },
  47. mounted () {
  48. this.$http.get('/api/carousel/home%20page%20banner')
  49. .then(response => {
  50. this.banners = response
  51. })
  52. },
  53. computed: {
  54. // banner () {
  55. // // return this.$store.state.carousel.banners
  56. // },
  57. activeColor () {
  58. if (this.banners.data) {
  59. return this.banners.data.length ? this.banners.data[this.activeSlide].metadata['background-color'] : null
  60. }
  61. }
  62. }
  63. }
  64. </script>
  65. <style lang="scss" scoped>
  66. @import '~assets/scss/variables';
  67. $carousel_width: 990px;
  68. $carousel_height: 477px;
  69. .carousel {
  70. transition: background-color .3s;
  71. position: relative;
  72. margin-bottom: $lg-pad;
  73. .carousel-container {
  74. width: $carousel_width;
  75. height: $carousel_height;
  76. margin-left: 200px;
  77. overflow: hidden;
  78. .swiper-wrapper {
  79. .swiper-slide {
  80. img {
  81. display: block;
  82. height: $carousel_height;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. </style>