当前位置:首页 > VUE

vue swiper实现图片轮播

2026-01-21 00:53:54VUE

安装 Swiper 依赖

在 Vue 项目中安装 Swiper 和对应的 Vue 封装库:

npm install swiper vue-awesome-swiper

引入 Swiper 组件和样式

在需要使用的组件中引入 Swiper 和样式文件:

import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/pagination';
import 'swiper/css/navigation';

基本轮播实现

在模板中使用 Swiper 组件,绑定图片数据:

<template>
  <swiper
    :slides-per-view="1"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="(image, index) in images" :key="index">
      <img :src="image.url" :alt="image.alt">
    </swiper-slide>
  </swiper>
</template>

<script>
export default {
  data() {
    return {
      images: [
        { url: 'image1.jpg', alt: 'Image 1' },
        { url: 'image2.jpg', alt: 'Image 2' },
        { url: 'image3.jpg', alt: 'Image 3' }
      ]
    }
  },
  methods: {
    onSwiper(swiper) {
      console.log(swiper);
    },
    onSlideChange() {
      console.log('slide change');
    }
  }
}
</script>

添加导航和分页

引入 Pagination 和 Navigation 模块并配置:

import { Pagination, Navigation } from 'swiper/modules';

export default {
  setup() {
    return {
      modules: [Pagination, Navigation],
    };
  },
  // 其他代码...
}

在模板中添加对应配置:

<swiper
  :slides-per-view="1"
  :space-between="50"
  :pagination="{ clickable: true }"
  :navigation="true"
  :modules="modules"
>
  <!-- swiper-slide内容 -->
</swiper>

自动播放配置

添加 Autoplay 模块并配置:

import { Autoplay } from 'swiper/modules';

export default {
  setup() {
    return {
      modules: [Autoplay],
    };
  },
  // 其他代码...
}

在模板中配置自动播放:

<swiper
  :autoplay="{
    delay: 2500,
    disableOnInteraction: false,
  }"
  :modules="modules"
>
  <!-- swiper-slide内容 -->
</swiper>

响应式配置

通过 breakpoints 属性实现不同屏幕尺寸下的不同显示:

vue swiper实现图片轮播

<swiper
  :breakpoints="{
    320: {
      slidesPerView: 1,
      spaceBetween: 20
    },
    768: {
      slidesPerView: 2,
      spaceBetween: 30
    },
    1024: {
      slidesPerView: 3,
      spaceBetween: 40
    }
  }"
>
  <!-- swiper-slide内容 -->
</swiper>

完整示例代码

<template>
  <swiper
    :slides-per-view="1"
    :space-between="50"
    :pagination="{ clickable: true }"
    :navigation="true"
    :autoplay="{ delay: 2500 }"
    :modules="modules"
    :breakpoints="breakpoints"
  >
    <swiper-slide v-for="(image, index) in images" :key="index">
      <img :src="image.url" :alt="image.alt">
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Pagination, Navigation, Autoplay } from 'swiper/modules';
import 'swiper/css';
import 'swiper/css/pagination';
import 'swiper/css/navigation';

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  setup() {
    return {
      modules: [Pagination, Navigation, Autoplay],
      breakpoints: {
        320: { slidesPerView: 1 },
        768: { slidesPerView: 2 },
        1024: { slidesPerView: 3 }
      }
    };
  },
  data() {
    return {
      images: [
        { url: 'image1.jpg', alt: 'Image 1' },
        { url: 'image2.jpg', alt: 'Image 2' },
        { url: 'image3.jpg', alt: 'Image 3' }
      ]
    };
  }
};
</script>

<style scoped>
.swiper {
  width: 100%;
  height: 300px;
}
.swiper-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

标签: 图片vue
分享给朋友:

相关文章

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="con…