Skip to content

触摸滑动插件 Swiper
^11.1.1


旋转木马,一组轮播的区域

何时使用

  • 当内容空间不足时,可以用走马灯的形式进行收纳,进行轮播展现
  • 常用于一组图片或卡片轮播

参考文档

基本使用

首页banner


Show Code
vue
<script setup lang="ts">
import { ref, onBeforeMount } from 'vue'

const images = ref<any[]>([])
function loadImages () {
  for (let i = 1; i <= 10; i++) {
    images.value.push({
      title: `image-${i}`,
      link: '',
      src: `https://cdn.jsdelivr.net/gh/themusecatcher/resources@0.0.5/${i}.jpg`
    })
  }
}
onBeforeMount(() => { // 组件已完成响应式状态设置,但未创建DOM节点
  loadImages()
})
function onChange () {
  console.log('slider change')
}
</script>
<template>
  <Swiper :images="images" :height="420" @change="onChange" />
</template>

走马灯

Show Code
vue
<script setup lang="ts">
import { ref, onBeforeMount } from 'vue'

const images = ref<any[]>([])
function loadImages () {
  for (let i = 1; i <= 10; i++) {
    images.value.push({
      title: `image-${i}`,
      link: '',
      src: `https://cdn.jsdelivr.net/gh/themusecatcher/resources@0.0.5/${i}.jpg`
    })
  }
}
onBeforeMount(() => { // 组件已完成响应式状态设置,但未创建DOM节点
  loadImages()
})
</script>
<template>
  <Swiper
    :images="images"
    type="carousel"
    :height="180"
    :slides-per-view="3"
    :space-between="20"
    :speed="2500" />
</template>

信息展播



Show Code
vue
<script setup lang="ts">
import { ref, shallowReactive, onBeforeMount } from 'vue'

const images = ref<any[]>([])
function loadImages () {
  for (let i = 1; i <= 10; i++) {
    images.value.push({
      title: `image-${i}`,
      link: '',
      src: `https://cdn.jsdelivr.net/gh/themusecatcher/resources@0.0.5/${i}.jpg`
    })
  }
}
onBeforeMount(() => { // 组件已完成响应式状态设置,但未创建DOM节点
  loadImages()
})
const navigation = shallowReactive<{[key: string]: any}>({})
function onSwiper (swiper: any) {
  navigation.prevEl = swiper.navigation.prevEl
  navigation.prevEl.style.display = 'none'
  navigation.nextEl = swiper.navigation.nextEl
  navigation.nextEl.style.display = 'none'
}
function onPrev () {
  navigation.prevEl.click()
}
function onNext () {
  navigation.nextEl.click()
}
</script>
<template>
  <Space>
    <Button @click="onPrev">Prev</Button>
    <Button @click="onNext">Next</Button>
  </Space>
  <br/>
  <br/>
  <Swiper
    :images="images"
    type="broadcast"
    :pagination="{
      dynamicBullets: true,
      clickable: true
    }"
    :height="200"
    :slides-per-view="3"
    :space-between="30"
    loop
    mousewheel
    @swiper="onSwiper" />
</template>

APIs

参数说明类型默认值必传
images轮播图片数组Image[][]true
width图片宽度number | string'100%'false
height图片高度number | string'100vh'false
typebanner: 轮播图模式;carousel: 走马灯模式;broadcast: 信息展播模式'banner' | 'carousel' | 'broadcast''banner'false
navigation是否显示导航booleantruefalse
delay自动切换的时间间隔(type: banner时生效),单位msnumber3000false
swipe是否可以鼠标拖动booleantruefalse
preloaderColor预加载时的 loading 颜色'theme' | 'white' | 'black''theme'false

Image Type

名称说明类型必传
title图片名称stringtrue
link图片跳转链接stringfalse
src图像地址stringtrue

Events

事件名称说明参数
swiperSwiper初始化后的回调(swiper: any) => void
change轮播图片变化时的回调() => void

Released under the MIT License.