我正在使用Owl Carousel 2并创建当前格式为<1/10>。我想实现两位数,所以对于小于10的数字,我需要在前面加一个0。理想情况下,格式如下:<01/10>。我尝试过if/then,但无法将其与计数器在此处的书写方式配合使用。有没有办法修改这个代码来实现这种格式?
JS/jQuery
<script>
$(function(){
var owl = $('.owl-carousel');
owl.owlCarousel({
items:1,
loop: false,
onInitialized : counter, //When the plugin has initialized.
onTranslated : counter //When the translation of the stage has finished.
});
$(".next").click(function() {
owl.trigger('next.owl.carousel');
});
$(".prev").click(function() {
owl.trigger('prev.owl.carousel');
});
function counter(event) {
var element = event.target;
var items = event.item.count; // Number of items
var item = event.item.index + 1; // Position of the current item
$('.counter').html(item+" / "+items)
}
});
</script>
HTML
<div class="gfoot">
<div><h5><a class="prev">< </a>
<span class="counter"></span>
<a class="next"> ></a></h5></div>
</div>
尝试替换此行:
$('.counter').html(item+" / "+items)
这行:
$('.counter').html((item < 10 ? '0' : '') + item + " / " + items)