VUE中实现轮播图——Swiper 插件应用 vue-awesome-swiper【详细步骤】

本文介绍了在Vue项目中使用Swiper插件创建轮播图的详细步骤,包括安装2.6.7版本的swiper,如何在main.js中引入,以及在组件间如何引用和使用。通过优化与改进,使用v-for动态显示轮播图的图片地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VUE中实现轮播图——Swiper vue-awesome-swiper 插件应用【详细步骤】


前言

Swiper(Swiper master)是目前应用较广泛的移动端网页触摸内容滑动js插件,是纯javascript打造的滑动特效插件,面向手机、平板电脑等移动终端;能实现触屏焦点图、触屏Tab切换、触屏多图切换等常用效果。本文将从介绍什么是Swiper插件,以及Swiper插件的使用方法等方面进行讲述如何在VUE项目中实现轮播图。


一、Swiper是什么?

Swiper(Swiper master)是目前应用较广泛的移动端网页触摸内容滑动js插件,是纯javascript打造的滑动特效插件,面向手机、平板电脑等移动终端;能实现触屏焦点图、触屏Tab切换、触屏多图切换等常用效果。

swiper是第一个第三方的库,可以用来实现移动端、pc端的滑动操作,十分方便
官方文档: https://www.swiper.com.cn/
在GitHub上参考安装和使用对策教程
网址:https://github.com/surmon-china/vue-awesome-swiper

二、使用步骤

1.安装

在这里插入图片描述

因为老版本会比新的更加稳定, 在这里我们就安装2.6.7这个版本

npm install swiper vue-awesome-swiper@2.6.7 --save

在这里插入图片描述

2.在VUE中引入swiper

我们要在main.js 中添加这两行代码引入swiper

import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/swiper-bundle.css'

3.在VUE中使用swiper

首先看一下我们的目录结构:
在这里插入图片描述
在home这个文件夹下面有components文件夹,以及Home.vue这个文件。
显而易见,我们需要使用到组件间的引用,在Home.vue引用swiper.vue这个子组件。

在GitHub上参考教程是这样写的
在这里插入图片描述
当然如果直接这样写的话,轮播的不是图片,而是数字,所以在这里我也做了一些小调整。
其实就是在<swiper-slide></swiper-slide>加一个img标签/

直接上Swiper.vue 的代码:
html部分:

<template>
<div class="wrapper">
    <swiper :options="swiperOptions">
    <
### 使用 `vue-awesome-swiper` 插件Vue 3 中创建轮播图 为了在 Vue 3 中使用 `vue-awesome-swiper` 创建轮播图,需先安装必要的依赖项。由于 `vue-awesome-swiper` 主要基于 Swiper 库构建,因此也需要安装 Swiper。 #### 安装依赖包 通过 npm 或 yarn 来安装所需的库: ```bash npm install vue-awesome-swiper swiper --save ``` 或者如果偏好使用 yarn: ```bash yarn add vue-awesome-swiper swiper ``` #### 配置项目以支持 ES Modules 和 Composition API 考虑到 Vue 3 的特性以及对 Composition API 的支持,在 main.js 文件中引入并注册全局组件之前,应该确保按照官方文档配置好环境[^1]。 #### 编写轮播图组件代码 下面是一个简单的轮播图实现例子,适用于 Vue 3 及其组合式API风格: ```javascript // src/components/Carousel.vue <template> <div class="swiper-container"> <!-- Additional required wrapper --> <div class="swiper-wrapper"> <div v-for="(slide, index) in slides" :key="index" class="swiper-slide">{{ slide }}</div> </div> <!-- If we need pagination --> <div class="swiper-pagination"></div> <!-- If we need navigation buttons --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> </div> </template> <script setup> import { ref } from 'vue'; import 'swiper/css/swiper.css'; const props = defineProps({ slides: { type: Array, default() { return ['Slide 1', 'Slide 2', 'Slide 3']; } }, }); let mySwiper; onMounted(() => { import('vue-awesome-swiper').then(({ Swiper }) => { mySwiper = new Swiper('.swiper-container', { loop: true, // 如果需要分页器 pagination: { el: '.swiper-pagination', }, // 如果需要前进后退按钮 navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }); }); }); </script> <style scoped> /* Add custom styles here */ .swiper-slide { text-align: center; font-size: 18px; background-color: #fff; } </style> ``` 此示例展示了如何利用 `vue-awesome-swiper` 结合 Vue 3 构建一个基本的轮播图,并包含了分页指示符和导航箭头的功能。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值