Vue2中使用vue-awesome-swiper创建轮播图幻灯片

本文主要是介绍Vue2中使用vue-awesome-swiper创建轮播图幻灯片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 一. 使用 vue-awesome-swiper 3.1.3 版本

https://github.com/surmon-china/vue-awesome-swiper/tree/v3.1.3https://github.com/surmon-china/vue-awesome-swiper/tree/v3.1.3

1. 安装 swiper 包和 vue-awesome-swiper 包

注意:这两个包缺一不可,且安装时都要指定版本号!!!不然会踩很多大坑!!!

这里用的swiper@4.5.1和vue-awesome-swiper@3.1.3 版本是相照应的,不可以使用其他版本!

npm install swiper@4 vue-awesome-swiper@3 --save  

2. 引入

引入方式可以参考官方文档,两种方式选一种即可:vue-awesome-swiper at v3.1.3

(1)第一种方式:在main.js入口文件中全局引入

// /src/main.js// swiper全局引入
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper, /* { default options with global component } */)

 (2)第二种方式:在需要使用轮播图的文件中按需引入

<script>
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'export default {components: {swiper,swiperSlide}
}
</script>

3. 使用轮播图

<template><div><swiper ref="mySwiper" :options="swiperOption"><!-- 三张轮播图 --><swiper-slide><img src="../assets/images/pic1.jpeg" alt=""></swiper-slide><swiper-slide><img src="../assets/images/pic2.jpeg" alt=""></swiper-slide><swiper-slide><img src="../assets/images/pic3.jpeg" alt=""></swiper-slide><!-- 分页器。如果放置在swiper外面,需要自定义样式。 --><div class="swiper-pagination" slot="pagination"></div></swiper></div>
</template><script>
// swiper已经全局引入过了,因此这里没有再引入export default {name: 'HomeView',data() {return {//swiper轮播swiperOption: {pagination: {el: '.swiper-pagination'}},}},}
</script>

踩坑:

vue-awesome-swiper @3.1.3使用,记录一些bug及解决方法_AIpoem的博客-CSDN博客安装、引入、需求:切换slide时要更换app组件的背景、问题:第一次进入时可以正常切换slide,切换路由之后,再切换回来就不能正常切换slide了、问题:默认显示的是最后一个swiper-slidehttps://blog.csdn.net/gegegegege12/article/details/121387965

vue-awesome-swiper 轮播图 自定义分页器 - 闰土的土 - 博客园首先最重要的是安装对应版本的swiper和vue-awesome-swiper,不然会有坑。 我安装的是"swiper": "^4.5.1","vuehttps://www.cnblogs.com/orzzt/p/15775075.html

4. 详细使用方法

教程见第09节--第11节:

技术胖-Vue2.x+Koa2实战移动电商(共61集)Vue2.x+Koa2实战移动电商(共61集)icon-default.png?t=M4ADhttps://www.jspang.com/article/61#toc10

<template><div><swiper class="swiper" :options="swiperOption"><swiper-slideclass="swiper-slide"v-for="(item, index) in slide":key="index">Slide {{ item }}</swiper-slide><!-- 分页器 --><div class="swiper-pagination" slot="pagination"></div></swiper></div>
</template><script>
import "swiper/dist/css/swiper.css";
import { swiper, swiperSlide } from "vue-awesome-swiper";
export default {data() {return {slide: [1, 2, 3, 4, 5, 6],swiperOption: {// 竖向direction: "vertical",// 循环滚动loop:true,//分页器pagination: {el: ".swiper-pagination",clickable:true},// 实现区域滚动效果// // 一屏中显示的swiper-slide数量// slidesPerView: "auto",// // 自由模式,一次不止滑动一屏,实现滚动效果// freeMode: true,// // 鼠标滚轮可以实现滚动// mousewheel: true,},};},components: { swiper, swiperSlide },
};
</script><style scoped>
.swiper-slide {height: 4rem;text-align: center;line-height: 4rem;
}
.swiper {height: 7rem;border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;
}
.text {font-size: 18px !important;text-align: left;padding: 30px;height: auto;-webkit-box-sizing: border-box;box-sizing: border-box;
}
</style>

二. 使用 vue-awesome-swiper 4.1.1 版本

GitHub - surmon-china/vue-awesome-swiper at v4.1.1https://github.com/surmon-china/vue-awesome-swiper/tree/v4.1.1

1. 安装 swiper 包和 vue-awesome-swiper 包

注意:这两个包缺一不可,且安装时都要指定版本号!!!不然会踩很多大坑!!!

这里用的swiper@5.4.5和vue-awesome-swiper@4.1.1版本是相照应的,不可以使用其他版本!

npm install swiper@5.4.5 vue-awesome-swiper@4.1.1 --save

2. 引入

引入方式可以参考官方文档,两种方式选一种即可:vue-awesome-swiper at v4.1.1

(1)第一种方式:在main.js入口文件中全局引入

// /src/main.js// swiper全局引入
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
Vue.use(VueAwesomeSwiper, /* { default options with global component } */)

(2)第二种方式:在需要使用轮播图的文件中按需引入

<script>
import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'export default {components: {Swiper,SwiperSlide},directives: {swiper: directive}
}
</script>

3. 使用轮播图

<template><swiper ref="mySwiper" :options="swiperOptions"><!-- 幻灯片 --><swiper-slide>Slide 1</swiper-slide><swiper-slide>Slide 2</swiper-slide><swiper-slide>Slide 3</swiper-slide><swiper-slide>Slide 4</swiper-slide><swiper-slide>Slide 5</swiper-slide><!-- 分页器 --><div class="swiper-pagination" slot="pagination"></div></swiper>
</template><script>export default {name: 'carrousel',data() {return {// swiper 轮播swiperOptions: {pagination: {el: '.swiper-pagination'},// Some Swiper option/callback...}}},computed: {swiper() {return this.$refs.mySwiper.$swiper}},mounted() {console.log('Current Swiper instance object', this.swiper)this.swiper.slideTo(3, 1000, false)}}
</script>

踩坑:

关于vue-awesome-swiper pagination组件失效后想到的旁门左道解决办法 - 简书项目做整屏滚动效果,使用 "vue-awesome-swiper": "^4.1.1"。发现pagination这分页器罢工,完全不显示,查阅好多资料,比较靠谱的解决办法就是...https://www.jianshu.com/p/be2bc71dcde2

这篇关于Vue2中使用vue-awesome-swiper创建轮播图幻灯片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/775494

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected