1.準(zhǔn)備js和css 訪問網(wǎng)址https://www.swiper.com.cn/ ,獲取Swiper,下載Swiper.
- 主要代碼html
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src=""/></div>
<div class="swiper-slide"><img src=""></div>
<div class="swiper-slide"><img src="" alt=""></div>
</div>
<!-- 如果需要分頁器 -->
<div class="swiper-pagination"></div>
</div>
3.在js中初始化swiper
var mySwiper = new Swiper ('.swiper-container', {
// 輪播圖的方向,也可以是vertical方向
direction:'horizontal',
//播放速度
loop: true,
// 自動播放時間
autoplay:true,
// 播放的速度
speed:2000,
// 如果需要分頁器,即下面的小圓點(diǎn)
pagination: {
el: '.swiper-pagination',
},
// 這樣,即使我們滑動之后, 定時器也不會被清除
autoplayDisableOnInteraction : false,
});
4.css樣式中
.swiper-container{
width: 100%;
height: 150px;
}
.swiper-slide{
width: 100%;
height: 150px;
}
.swiper-slide img{
width: 100%;
height: 100%;
}