I am currently in the process of developing a new website, and we are looking to randomize a group of DIV elements within a document that have a specific class called 'shuffle':
(function($){
$.fn.shuffle = function() {
var allElems = this.get(),
getRandom = function(max) {
return Math.floor(Math.random() * max);
},
shuffled = $.map(allElems, function(){
var random = getRandom(allElems.length),
randEl = $(allElems[random]).clone(true)[0];
allElems.splice(random, 1);
return randEl;
});
this.each(function(i){
$(this).replaceWith($(shuffled[i]));
});
return $(shuffled);
};
})(jQuery);
The structure of my DIV elements is as follows:
<div class="row animate-in shuffle" data-anim-type="fade-in-up">
<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<div class="team-wrapper">
<div class="team-inner" style="background-image: url('assets/img/team/1.jpg')">
<!-- <a href="#" target="_blank" > <i class="fa fa-twitter" ></i></a> -->
</div>
<div class="description">
<h3> Test User 1</h3>
<h5><strong> Tester - Inhaber </strong></h5>
<p>
fon: 1234567890<br>
<a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe8a9b8d8abe8a9b8d8ad09a9b">[email protected]</a>">e-mail: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe8a9b8d8abe8a9b8d8ad09a9b">[email protected]</a></a><br>
</p>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<div class="team-wrapper">
<div class="team-inner" style="background-image: url('assets/img/team/2.jpg')">
<!-- <a href="#" target="_blank" > <i class="fa fa-twitter" ></i></a> -->
</div>
<div class="description">
<h3> Test User 2</h3>
<h5><strong> Tester </strong></h5>
<p>
fon: 0987654321<br>
<a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee9a8b9d9a8b9cdcae9a8b9d9ac08a8b">[email protected]</a>">e-mail: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="493d2c3a3d2c3b7b093d2c3a3d672d2c">[email protected]</a></a><br>
</p>
</div>
</div>
</div>
</div>
Here's how I initiate the shuffling on page load:
$('div#shuffle div').shuffle();
Despite implementing the shuffle function, the elements remain in the same order as they appear in the document. Can anyone point out where I might be going wrong?
Appreciate any insights provided :)