使用Slick.js - 如何获得当前和总幻灯片(即3/5)作为点的简单替代方法?
我被告知我可以使用回调参数对象来使用customPaging
回调,但这究竟是什么意思?
$('.slideshow').slick({
slide: 'img',
autoplay: true,
dots: true,
customPaging: function (slider, i) {
return slider.slickCurrentSlide + '/' + (i + 1);
}
});
答案 0 :(得分:54)
slider
对象包含一个包含幻灯片计数的变量。
$('.slideshow').slick({
slide: 'img',
autoplay: true,
dots: true,
dotsClass: 'custom_paging',
customPaging: function (slider, i) {
//FYI just have a look at the object to find available information
//press f12 to access the console in most browsers
//you could also debug or look in the source
console.log(slider);
return (i + 1) + '/' + slider.slideCount;
}
});
<强> DEMO 强>
var $status = $('.pagingInfo');
var $slickElement = $('.slideshow');
$slickElement.on('init reInit afterChange', function(event, slick, currentSlide, nextSlide){
//currentSlide is undefined on init -- set it to 0 in this case (currentSlide is 0 based)
var i = (currentSlide ? currentSlide : 0) + 1;
$status.text(i + '/' + slick.slideCount);
});
$slickElement.slick({
slide: 'img',
autoplay: true,
dots: true
});
var $status = $('.pagingInfo');
var $slickElement = $('.slideshow');
$slickElement.on('init reInit afterChange', function(event, slick, currentSlide, nextSlide){
//currentSlide is undefined on init -- set it to 0 in this case (currentSlide is 0 based)
var i = (currentSlide ? currentSlide : 0) + 1;
$status.text(i + '/' + slick.slideCount);
});
$slickElement.slick({
autoplay: true,
dots: true
});
答案 1 :(得分:3)
这可能会有所帮助:
<强> CSS 强>
.slick-counter{
position:absolute;
top:5px;
left:5px;
background:yellow;
padding:5px;
opacity:0.8;
border-radius:5px;
}
<强>的JavaScript 强>
var $el = $('.slideshow');
$el.slick({
slide: 'img',
autoplay: true,
onInit: function(e){
$el.append('<div class="slick-counter">'+ parseInt(e.currentSlide + 1, 10) +' / '+ e.slideCount +'</div>');
},
onAfterChange: function(e){
$el.find('.slick-counter').html(e.currentSlide + 1 +' / '+e.slideCount);
}
});
答案 2 :(得分:3)
我使用此代码并且有效:
.slider - 这是滑块
.count - 用于返回计数器的选择器
$(".slider").on("init", function(event, slick){
$(".count").text(parseInt(slick.currentSlide + 1) + ' / ' + slick.slideCount);
});
$(".slider").on("afterChange", function(event, slick, currentSlide){
$(".count").text(parseInt(slick.currentSlide + 1) + ' / ' + slick.slideCount);
});
$(".page-article-item_image-slider").slick({
slidesToShow: 1,
arrows: true
});
答案 3 :(得分:2)
基于@Mx发布的第一个命题。 (2014年9月15日发布)我创建了一个变体来为ARIA支持获得一个不错的HTML标记。
$('.slideshow').slick({
slide: 'img',
autoplay: true,
dots: true,
dotsClass: 'custom_paging',
customPaging: function (slider, i) {
//FYI just have a look at the object to find available information
//press f12 to access the console in most browsers
//you could also debug or look in the source
console.log(slider);
var slideNumber = (i + 1),
totalSlides = slider.slideCount;
return '<a class="custom-dot" role="button" title="' + slideNumber + ' of ' + totalSlides + '"><span class="string">' + slideNumber + '</span></a>';
}
});
<强> jsFiddle DEMO 强>
答案 4 :(得分:2)
您需要在初始化之前绑定init。
$('.slider-for').on('init', function(event, slick){
$(this).append('<div class="slider-count"><p><span id="current">1</span> von <span id="total">'+slick.slideCount+'</span></p></div>');
});
$('.slider-for').slick({
slidesToShow: 1,
slidesToScroll: 1,
arrows: true,
fade: true
});
$('.slider-for')
.on('afterChange', function(event, slick, currentSlide, nextSlide){
// finally let's do this after changing slides
$('.slider-count #current').html(currentSlide+1);
});
答案 5 :(得分:2)
使用前面的方法,一次超过1张幻灯片给了我错误的总数,所以我使用了“dotsClass”,就像这样(在v1.7.1上):
// JS
var slidesPerPage = 6
$(".slick").on("init", function(event, slick){
maxPages = Math.ceil(slick.slideCount/slidesPerPage);
$(this).find('.slider-paging-number li').append('/ '+maxPages);
});
$(".slick").slick({
slidesToShow: slidesPerPage,
slidesToScroll: slidesPerPage,
arrows: false,
autoplay: true,
dots: true,
infinite: true,
dotsClass: 'slider-paging-number'
});
// CSS
ul.slider-paging-number {
list-style: none;
li {
display: none;
&.slick-active {
display: inline-block;
}
button {
background: none;
border: none;
}
}
}
答案 6 :(得分:0)
Modifications are done to the new Slick version 1.7.1.
以下是更新的脚本示例:jsfiddle
答案 7 :(得分:0)
如果要进行页码编号:
示例: 1 2 3 4 ...
HTML:
<div class="slider">
<div>
<div>Some content</div>
<div class="slider-number"><span>1 2 3 4...</span></div>
</div>
<div>
<div>Some content</div>
<div class="slider-number"><span>1 2 3 4...</span></div>
</div>
...
...
</div>
JS:
$('.slider').on('init reInit afterChange',
function(event, slick, currentSlide){
var status = $(this).find('.slider-number span');
//currentSlide is undefined on init -- set it to 0 in this case (currentSlide is 0 based)
var i = slick.currentSlide;
var slidesLength = slick.slideCount;
var numberSlide1 = i + 1 <= slidesLength ? i + 1 : i - (slidesLength - 1);
var numberSlide2 = i + 2 <= slidesLength ? i + 2 : i - (slidesLength - 2);
var numberSlide3 = i + 3 <= slidesLength ? i + 3 : i - (slidesLength - 3);
var numberSlide4 = i + 4 <= slidesLength ? i + 4 : i - (slidesLength - 4);
status.html('<strong>'+numberSlide1+'</strong>' + ' ' +
numberSlide2 + ' ' +
numberSlide3 + ' ' +
numberSlide4 + '...');
});
答案 8 :(得分:0)
我遇到了多张幻灯片的问题。版本1.8.1
如果slidesToShow
和slidesToScroll
多于1
技巧在slick.slickGetOption('slidesToShow');
$(".your-selector").on('init reInit afterChange', function(event, slick, currentSlide, nextSlide){
var i = (currentSlide ? currentSlide : 0) + 1;
var slidesToShow = slick.slickGetOption('slidesToShow');
var curPage = parseInt((i-1)/slidesToShow) + 1;
var lastPage = parseInt((slick.slideCount-1)/slidesToShow) + 1;
$('.your-selector').text(curPage);
$('.your-selector').text(lastPage);
});
注意curPage
和lastPage
是分开的。我必须给它们涂上不同的颜色。
基于投票最多的答案