一、swiper网址
二、下载swiper
2.1、传统方式zip下载
2.2、npm下载(推荐)
npm install swiper
三、使用
3.1、找到js和css依赖
解压下载好的文件,找到swiper-bundle.min.js和swiper-bundle.min.css文件
3.2、依赖引入并初始化swiper
注意js文件要在body后面引入,原因有两点:
1、因为浏览器在渲染html的时候,从上到下依次执行,遇到js文件就会停止当前页面的渲染,转而去下载js文件,如果将script标签放在头部,如果文件又很大的情况下,首屏时间就会延长,影响用户体验。
2、初始化swiper的方法要在dom渲染完成的时候才可以执行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- 引入css -->
<link rel="stylesheet" href="./css/swiper-bundle.min.css" />
<title>swiper</title>
</head>
<body>
<!-- 引入js -->
<script src="./js/swiper-bundle.min.js"></script>
<script>
var mySwiper = new Swiper(".swiper", {
// direction: "vertical", // 垂直切换选项(默认水平)
loop: true, // 循环模式选项
// 如果需要分页器
pagination: {
el: ".swiper-pagination",
},
// 如果需要前进后退按钮
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
// 如果需要滚动条
// scrollbar: {
// el: ".swiper-scrollbar",
// },
});
</script>
</body>
</html>
3.3、html结构
<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<!-- 如果需要分页器 -->
<div class="swiper-pagination"></div>
<!-- 如果需要导航按钮 -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<!-- 如果需要滚动条 -->
<!-- <div class="swiper-scrollbar"></div> -->
</div>
3.4、css样式
你可能想要给Swiper定义一个大小,当然不要也行。
* {
margin: 0;
padding: 0;
}
.swiper {
width: 100%;
height: 300px;
}
3.5、完成。恭喜你,现在你的Swiper应该已经能正常切换了。
四、如果作为CommonJs 或ES 模块引入
//CommonJs
var Swiper = require('swiper');
var mySwiper = new Swiper('.swiper', { /* ... */ });
//ES
import Swiper from 'swiper';
var mySwiper = new Swiper('.swiper', { /* ... */ });
五、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./css/swiper-bundle.min.css" />
<title>swiper</title>
<style>
* {
margin: 0;
padding: 0;
}
.swiper {
width: 100%;
height: 300px;
}
.swiper-slide img {
width: 100%;
}
</style>
</head>
<body>
<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="./imgs/img1.jpeg" alt="" /></div>
<div class="swiper-slide"><img src="./imgs/img2.webp" alt="" /></div>
<div class="swiper-slide"><img src="./imgs/img3.jpg" alt="" /></div>
</div>
<!-- 如果需要分页器 -->
<div class="swiper-pagination"></div>
<!-- 如果需要导航按钮 -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<!-- 如果需要滚动条 -->
<!-- <div class="swiper-scrollbar"></div> -->
</div>
</body>
<script src="./js/swiper-bundle.min.js"></script>
<script>
var mySwiper = new Swiper(".swiper", {
// direction: "vertical", // 垂直切换选项(默认水平)
loop: true, // 循环模式选项
// 如果需要分页器
pagination: {
el: ".swiper-pagination",
},
// 如果需要前进后退按钮
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
// 如果需要滚动条
// scrollbar: {
// el: ".swiper-scrollbar",
// },
});
</script>
</html>