Web API(7)

微信扫一扫,分享到朋友圈

Web API(7)

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="https://www.tuicool.com/articles/UBvyIbB/dist/css/**swiper-bundle.min.css**">
</head>
<body>
...
<script src="https://www.tuicool.com/articles/UBvyIbB/dist/js/**swiper-bundle.min.js**"></script>
...
</body>
</html>

2.HTML内容。

<div>
<div>
<div>Slide 1</div>
<div>Slide 2</div>
<div>Slide 3</div>
</div>
<!-- 如果需要分页器 -->
<div></div>
<!-- 如果需要导航按钮 -->
<div></div>
<div></div>
<!-- 如果需要滚动条 -->
<div></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="https://www.tuicool.com/articles/UBvyIbB/swiper-6.1.2/package/swiper-bundle.css" />
<script src="https://www.tuicool.com/articles/UBvyIbB/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>
<div>
<div><img src="https://www.tuicool.com/articles/UBvyIbB/img/focus1.jpg"></div>
<div><img src="https://www.tuicool.com/articles/UBvyIbB/img/focus2.jpg"></div>
<div><img src="https://www.tuicool.com/articles/UBvyIbB/img/focus3.jpg"></div>
<div><img src="https://www.tuicool.com/articles/UBvyIbB/img/focus4.jpg"></div>
</div>
<!-- Add Pagination -->
<div></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="https://www.tuicool.com/articles/UBvyIbB/css/swiper-bundle.css" />
<script src="https://www.tuicool.com/articles/UBvyIbB/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>
<div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<!-- Add Pagination -->
<div></div>
</div>
<!-- Swiper JS -->
<script src="https://www.tuicool.com/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代码

SuperSlider插件

微信扫一扫,分享到朋友圈

Web API(7)

今晚发布!华为FreeBuds Pro真机照再曝:这次好看多了

上一篇

FMI、SSP、DCP简介

下一篇

你也可能喜欢

Web API(7)

长按储存图像,分享给朋友