Shuffle DOM element using jquery andrey четверг, 10 апреля 2014 г. No Comment

Source from James Padolsey.  In this post, I just Created a Demo using his code.
$.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);
};
Usage of Code.

$('selector').shuffle();
Here is the DEMO,
You can find pure javascript way also from his article
Source from James Padolsey.  In this post, I just Created a Demo using his code.
$.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);
};
Usage of Code.

$('selector').shuffle();
Here is the DEMO,
You can find pure javascript way also from his article
by Jillur Rahman

Jillur Rahman is a Web designers. He enjoys to make blogger templates. He always try to make modern and 3D looking Templates. You can by his templates from Themeforest.

Follow him @ Twitter | Facebook | Google Plus

No Comment