swiper自定義分頁器的樣式
本文實例為大家分享了swiper自定義分頁器的樣式代碼,供大家參考,具體內(nèi)容如下

js主要代碼
pagination: {
// 自定義分頁器的類名----必填項
el: '.custom-pagination',
// 是否可點擊----必填項
clickable: true,
// 分頁的類型是自定義的----必填項
type: 'custom',
// 自定義特殊類型分頁器,當分頁器類型設(shè)置為自定義時可用。
renderCustom: function (swiper, current, total) {
console.log(current);//1 2 3 4
}
},
一、el
分頁器容器的css選擇器或HTML標簽。分頁器等組件可以置于container之外,不同Swiper的組件應該有所區(qū)分,如#pagination1,#pagination2。
二、分頁器樣式類型 可選
‘bullets' 圓點(默認)
‘fraction' 分式
‘progressbar' 進度條
‘custom' 自定義
三、renderCustom()
自定義特殊類型分頁器,當分頁器類型設(shè)置為自定義時可用。
四、slideToLoop()
在Loop模式下Swiper切換到指定slide。切換到的是slide索引是realIndex
源碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 1、CDN引入文件 -->
<link rel="stylesheet" >
<script src="https://unpkg.com/swiper/swiper-bundle.js"> </script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.2.1.min.js"></script>
<!--2、文件自己引入 -->
<!-- <script src="swiper.min.js"></script>
<link rel="stylesheet" href="swiper.min.css" rel="external nofollow" >
<script src="jquery.js"></script> -->
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
text-align: center;
overflow: hidden;
}
a {
text-decoration: none;
color: #000;
}
.swiper-content {
width: 80%;
overflow: hidden;
margin: 0 auto;
}
.swiper-content .title {
height: 100px;
display: flex;
align-items: center;
justify-content: space-around;
}
.swiper-content .nav-item {
float: left;
display: block;
margin-right: 30px;
width: 50px;
height: 30px;
line-height: 30px;
}
/* 點擊的激活樣式 */
.swiper-content .nav-item.active {
background-color: #378ee6;
}
/* 輪播圖的樣式 */
.swiper-content .swiper-container {
height: 300px;
background-color: pink;
}
</style>
</head>
<body>
<div class="swiper-content">
<!-- 自定義導航 -->
<div class="title">
<!-- 給ul 添加自定義分頁器類名 custom-pagination -->
<ul class="nav custom-pagination">
<li class="nav-item active">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>
</li>
<li class="nav-item">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >網(wǎng)站</a>
</li>
<li class="nav-item">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >關(guān)于</a>
</li>
<li class="nav-item">
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >聯(lián)系</a>
</li>
</ul>
</div>
<!-- 輪播圖 -->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<h1>1</h1>
</div>
<div class="swiper-slide">
<h1>2</h1>
</div>
<div class="swiper-slide">
<h1>3</h1>
</div>
<div class="swiper-slide">
<h1>4</h1>
</div>
</div>
</div>
</div>
<script>
var mySwiper = new Swiper('.swiper-container',
{
// 循環(huán)模式
loop: true,
pagination: {
// 自定義分頁器的類名----必填項
el: '.custom-pagination',
// 是否可點擊----必填項
clickable: true,
// 分頁的類型是自定義的----必填項
type: 'custom',
// 自定義特殊類型分頁器,當分頁器類型設(shè)置為自定義時可用。
renderCustom: function (swiper, current, total) {
// console.log(current);//1 2 3 4
// 1、分頁器激活樣式的改變---給自己添加激活樣式并將兄弟的激活樣式移出。
$('.custom-pagination').children().eq(current - 1).addClass('active').siblings().removeClass('active');
// 2、分頁器點擊的時候同時切換輪播圖(事件委托)----循環(huán)模式slideToLoop--
$('.custom-pagination').on('click', 'li', function () {
mySwiper.slideToLoop($(this).index(), 1000, false)
})
}
},
})
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript知識點總結(jié)(十六)之Javascript閉包(Closure)代碼詳解
閉包是可以包含自由(未綁定)變量的代碼塊;這些變量不是在這個代碼塊或者任何全局上下文中定義的,而是在定義代碼塊的環(huán)境中定義。本文主要介紹了javascript中的閉包,感興趣的朋友一起看看吧2016-05-05

