有一个Swiper滑块,现在分页看起来像1-3,我需要像01-03。
这是一个演示
https://codepen.io/anakin-skywalker94/pen/RmWxbE
HTML
<!-- Slider main container -->
<div class="swiper-container">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<!-- If we need pagination -->
<div class="swiper-pagination"></div>
</div>
JS
var swiper = new Swiper('.swiper-container', {
pagination: {
el: '.swiper-pagination',
type: 'fraction',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
direction: 'horizontal',
loop: true,
slidesPerView: 1,
spaceBetween: 0,
mousewheel: true,
pagination: {
el: '.swiper-pagination',
type: 'fraction',
clickable: true,
renderFraction: function (currentClass, totalClass) {
return '<span class="' + currentClass + '"></span>' + ' <span>-</span> ' + '<span class="' + totalClass + '"></span>'; }
},
});
谢谢!
答案 0 :(得分:1)
您可以使用类似('0' + 4).slice(-2)
的名称,以便将0
添加到小于10
且大于0
的数字之前。
所以您的JS看起来像这样:
var swiper = new Swiper('.swiper-container', {
pagination: {
el: '.swiper-pagination',
type: 'custom',
renderCustom: function (swiper, current, total) {
return ('0' + current).slice(-2) + ' of ' + ('0' + total).slice(-2);
}
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
direction: 'horizontal',
loop: true,
slidesPerView: 1,
spaceBetween: 0,
mousewheel: true,
renderCustom: function (swiper, current, total) {
return current + ' of ' + total;
}
});
请参阅有关Codepen的有效演示:https://codepen.io/Orlandster/pen/jobZmz
答案 1 :(得分:0)
var swiper = new Swiper('.swiper-container', {
pagination: {
el: '.swiper-pagination',
type: 'fraction',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
direction: 'horizontal',
loop: true,
slidesPerView: 1,
spaceBetween: 0,
mousewheel: true,
pagination: {
el: '.swiper-pagination',
type: 'custom',
clickable: true,
renderCustom: function (swiper, current, total) {
function numberAppend(d) {
return (d < 10) ? '0' + d.toString() : d.toString();
}
return numberAppend(current) + ' of ' + numberAppend(total);
}
},
});
.swiper-slide {
width: 100%;
height: 400px;
background: yellow;
}
.swiper-container {
height:300px
}
<link href="https://idangero.us/swiper/dist/css/swiper.min.css" rel="stylesheet"/>
<script src="https://idangero.us/swiper/dist/js/swiper.min.js"></script>
<!-- Slider main container -->
<div class="swiper-container">
<!-- Additional required wrapper -->
<div class="swiper-wrapper">
<!-- Slides -->
<div class="swiper-slide">Slide 01</div>
<div class="swiper-slide">Slide 02</div>
<div class="swiper-slide">Slide 03</div>
</div>
<!-- If we need pagination -->
<div class="swiper-pagination"></div>
</div>
答案 2 :(得分:0)
由于其他答案不再有效,这里有一个结构稍微简化的工作变体,适用于最新的 swiper 版本:
var swiper = new Swiper('.swiper-container', {
effect: 'fade',
pagination: {
el: '.swiper-pagination',
type: 'fraction',
formatFractionCurrent: function (number) {
return ('0' + number).slice(-2);
},
formatFractionTotal: function (number) {
return ('0' + number).slice(-2);
},
renderFraction: function (currentClass, totalClass) {
return '<span class="' + currentClass + '"></span>' +
' - ' +
'<span class="' + totalClass + '"></span>';
}
},
});
.swiper-container {
width: 100%;
height: 100px;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
background: gray;
/* 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;
}
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
<link href="https://unpkg.com/swiper/swiper-bundle.min.css" rel="stylesheet"/>
<!-- Swiper -->
<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 class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
<!-- Add Pagination -->
<div class="swiper-pagination"></div>
</div>