En utilisant Slick.js - comment obtenir des diapositives actuelles et totales (c'est-à-dire 3/5) comme une alternative plus simple aux points?
On m'a dit que je pouvais utiliser le rappel customPaging
en utilisant les objets d'argument de rappel, mais qu'est-ce que cela signifie exactement?
$('.slideshow').slick({
slide: 'img',
autoplay: true,
dots: true,
customPaging: function (slider, i) {
return slider.slickCurrentSlide + '/' + (i + 1);
}
});
L'objet slider
contient une variable contenant le nombre de diapositives.
$('.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;
}
});
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
});
J'utilise ce code et ça marche:
.slider - c'est un slider block
.count - sélecteur utilisé pour le compteur de retours
$(".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
});
Cela pourrait aider:
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);
}
});
Vous devez lier init avant l'initialisation.
$('.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);
});
Basé sur la première proposition publiée par @Mx. (posté le 15 septembre 2014) J'ai créé une variante pour obtenir un balisage HTML correct pour ARIA support.
$('.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>';
}
});
Utiliser la méthode précédente avec plus d'une diapositive à la fois me donnait un total erroné. J'ai donc utilisé la "dotsClass", comme ceci (sur la 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;
}
}
}
si vous voulez numéroter les pages:
Exemple: 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 + '...');
});
Modifications are done to the new Slick version 1.7.1.
Voici un exemple de script mis à jour: jsfiddle