如何在Vuetify中制作带有多个图像的轮播?

时间:2018-07-06 11:31:11

标签: vuetify.js

我想制作一个带有多个图像的轮播,

Multiple images carousel

它应该在单击箭头时滑动单个图像。

有一个feature request,但是在固定之前如何制作呢?

2 个答案:

答案 0 :(得分:3)

如果您希望对轮播的外观和功能进行更细粒度的控制 (触摸,速度,动量,每张幻灯片的图像数量,响应行为...),并且不想从头开始实现它,建议您将Vue-Awesome-Swiper用作Vue插件,并在出现以下情况时远离Vuetify涉及轮播和图片库,至少在这方面更完善之前。

围绕功能强大的Swiper.js库的薄Vue包装器中的Vue-Awesome-Swiper,使用开发人员自己的话来说就是:

  

具有硬件加速过渡功能的最现代的移动触摸滑块

这是带有多个图像的轮播的屏幕截图:

horizontal carousel

Codepen demo

这是带有拇指的图库的屏幕截图:

image gallery with thumbs

Codepen demo

以下是第一个屏幕截图的代码段:

Vue.use(VueAwesomeSwiper);
new Vue({
  el: "#app",
  data: () => ({
    swiperSlides: [
      "pink",
      "red",
      "yellow",
      "purple",
      "green",
      "orange",
      "blue"
    ],
    swiperOption: {
      initialSlide: 0,
      slidesPerView: 4,
      spaceBetween: 20,
      freeMode: true,
      watchOverflow: true,
      breakpoints: {
        1904: {
          slidesPerView: 4,
          spaceBetween: 20
        },
        1264: {
          slidesPerView: 4,
          spaceBetween: 20
        },
        960: {
          slidesPerView: 4,
          spaceBetween: 50
        },
        600: {
          slidesPerView: 3,
          spaceBetween: 150
        },
        400: {
          slidesPerView: 2,
          spaceBetween: 150
        }
      },
      pagination: {
        el: ".swiper-pagination",
        clickable: true
      },
      navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev"
      }
    }
  }),
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
  mounted() {
    // current swiper instance
    this.swiper.slideTo(3, 1000, false);
  },
  methods: {
    callback() {
      //read the API docs to see which slider events are available
    }
  }
});
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify/dist/vuetify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/js/swiper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-awesome-swiper@3.1.2/dist/vue-awesome-swiper.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css" rel="stylesheet" />
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet" />

<div id="app">
  <v-app>
    <v-container>
      <v-card id="product">
        <swiper :options="swiperOption" ref="mySwiper" @someSwiperEvent="callback">
          <!-- slides -->
          <swiper-slide v-for="(slide, index) in swiperSlides" :key="index">
            <v-avatar tile="tile" size="150px" :color="slide">
              <img src="https://www.dropbox.com/s/a8813qmu9f5yixz/shoe_left.png?raw=1">
            </v-avatar>
          </swiper-slide>

          <!-- Optional controls -->
          <div class="swiper-pagination" slot="pagination"></div>
          <div class="swiper-button-prev" slot="button-prev"></div>
          <div class="swiper-button-next" slot="button-next"></div>
        </swiper>
      </v-card>
    </v-container>
  </v-app>
</div>

如果您使用的是vue-cli,则可以在主js文件中导入Vue-Awesome-Swiper,并将其注册为Vue插件,就像使用Vuetify一样:

import VueAwesomeSwiper from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
Vue.use(VueAwesomeSwiper);

希望对您有帮助。

答案 1 :(得分:2)

我发现了以下解决方法:

<v-carousel>
  <v-carousel-item :key="i" v-for="i in 5">
    <v-layout row>
      <v-flex xs4 :key="j" v-for="j in 3">
        <img :src="'https://placehold.it/380x500/?text=' + i + '-' + j" alt="">
      </v-flex>
    </v-layout>
  </v-carousel-item>
</v-carousel>

但是它一次滚动一组图像。参见demo

更新:您可以使用Vue2-Siema,它是Siema(轻量级轮播库)的Vue 2包装器。参见demo