Swiper 插件的应用
中武官网地址:https://www.swiper.com.cn/
1. 引入插件相干文件
2. 依照规定语法应用
留神:要引入 swiper 中的 css 文件 js 文件 html 构造等,不要随便更改类名
Swiper 的应用办法
1. 首先加载插件,须要用到的文件有 swiper-bundle.min.js 和 swiper-bundle.min.css 文件,不同 Swiper 版本用到的文件名略有不同。可下载 Swiper 文件或应用 CDN。
<!DOCTYPE html>
<html>
<head>
...
<link rel="stylesheet" href="dist/css/**swiper-bundle.min.css**">
</head>
<body>
...
<script src="dist/js/**swiper-bundle.min.js**"></script>
...
</body>
</html>
2.HTML 内容。
<div class="swiper-container">
<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>
导航等组件能够放在 container 之外
3. 你可能想要给 Swiper 定义一个大小,当然不要也行。
.swiper-container {
width: 600px;
height: 300px;
}
4. 初始化 Swiper
<script> var mySwiper = new Swiper ('.swiper-container', {
direction: 'vertical', // 垂直切换选项
loop: true, // 循环模式选项
// 如果须要分页器
pagination: {el: '.swiper-pagination',},
// 如果须要后退后退按钮
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
// 如果须要滚动条
scrollbar: {el: '.swiper-scrollbar',},
}) </script>
5. 实现。祝贺你,当初你的 Swiper 应该曾经能失常切换了。
如果作为 CommonJs 或 ES 模块引入
//CommonJs
var Swiper = require('swiper');
var mySwiper = new Swiper('.swiper-container', { /* ... */});
//ES
import Swiper from 'swiper';
var mySwiper = new Swiper('.swiper-container', { /* ... */});
轮播图 1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> 轮播图 </title>
<link rel="stylesheet" type="text/css" href="swiper-6.1.2/package/swiper-bundle.css" />
<script src="swiper-6.1.2/package/swiper-bundle.js"></script>
<style>
html,
body {
position: relative;
height: 100%;
}
body {
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
}
.swiper-container {
width: 990px height: 540px;
margin: 200px auto;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
img {
width: 990px;
height: 540px;
}
.swiper-pagination-bullet {
width: 20px;
height: 10px;
border-radius: 0px;
background-color: white;
}
</style>
</head>
<body>
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="img/focus1.jpg"></div>
<div class="swiper-slide"><img src="img/focus2.jpg"></div>
<div class="swiper-slide"><img src="img/focus3.jpg"></div>
<div class="swiper-slide"><img src="img/focus4.jpg"></div>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>
</body>
<script>
var swiper = new Swiper('.swiper-container', {
spaceBetween: 30,
centeredSlides: true,
autoplay: {
delay: 2000,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
</script>
</html>
轮播图 2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Swiper demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<!-- Link Swiper's CSS -->
<link rel="stylesheet" type="text/css" href="css/swiper-bundle.css" />
<script src="js/swiper-bundle.js"></script>
<!-- Demo styles -->
<style>
body {
background: #fff;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
background-color: rgba(0, 0, 0, 0.5);
}
.swiper-container {
width: 100%;
padding-top: 50px;
padding-bottom: 50px;
}
.swiper-slide {
background-position: center;
background-size: cover;
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<!-- Swiper -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" style="background-image:url(./img/focus1.jpg)"></div>
<div class="swiper-slide" style="background-image:url(./img/focus2.jpg)"></div>
<div class="swiper-slide" style="background-image:url(./img/focus3.jpg)"></div>
<div class="swiper-slide" style="background-image:url(./img/focus4.jpg)"></div>
<div class="swiper-slide" style="background-image:url(./img/focus1.jpg)"></div>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>
<!-- Swiper JS -->
<script src="../package/swiper-bundle.min.js"></script>
<!-- Initialize Swiper -->
<script>
window.addEventListener('DOMContentLoaded', function() {
var swiper = new Swiper('.swiper-container', {
effect: 'coverflow',
grabCursor: true,
centeredSlides: true,
slidesPerView: 'auto',
coverflowEffect: {
rotate: 50,
stretch: 0,
depth: 100,
modifier: 1,
slideShadows: true,
},
pagination: {el: '.swiper-pagination',},
});
})
</script>
</body>
</html>
插件应用总结
1. 确认插件实现的性能
2. 去官网查看应用阐明
3. 下载插件
4. 关上 demo 实例文件,查看须要引入的相干文件,并且引入
5. 复制 demo 实例文件中的构造 html,款式 css 以及 js 代码