I am having trouble with a piece of code (below and pulled from https://stackoverflow.com/questions/31459832/randomise-and-order-divs-slick-js) which essentially randomises a set of slides for a hero slider (using slick.js). This particular snippet randomises the slides but keeps the last slide in order at the end of the slideshow.
The piece which is confusing me, and the piece which I am sure is pertinent to my problem is the bind/detach/append sequence. What I am aiming for is to keep the first slide in place at the beginning, not the last slide in place at the end. I assume the if statement should be checking for 0, not the array length-1... The confusion for me is, as mentioned, the bind/detach/append.
If anyone can shed some light on the direction i should be heading it would be much appreciated. My Javascript is a little rusty as I am sure you can tell!
Many Thanks in advance.
JJ
$.fn.randomize = function (selector) {
var $elems = selector ? $(this).find(selector) : $(this).children(),
$parents = $elems.parent();
$parents.each(function () {
$(this).children(selector).sort(function (childA, childB) {
// * Prevent last slide from being reordered
if($(childB).index() !== $(this).children(selector).length - 1) {
return Math.round(Math.random()) - 0.5;
}
}.bind(this)).detach().appendTo(this);
});
return this;
};
$('#carousel-hero .carousel-inner').randomize().slick({
speed: 3000,
dots: true,
fade: true,
autoplay: true,
autoplaySpeed: 3000,
prevArrow: '<button type="button" data-role="none" class="carousel-control left slick-prev">Previous</button>',
nextArrow: '<button type="button" data-role="none" class="carousel-control right slick-next">Next</button>',
});
}