当前位置:首页 > 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 属性实现不同屏幕尺寸下的不同显示:

<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 swiper实现图片轮播

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

相关文章

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template>…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…