jquery.randomize.js
plugin based off of jquery.reorder.js without cloning or element removal, and the ability to define callbacks for after an element has been randomized and when randomization is done. Soon with effects.
by thinkaxelthink
March 02, 2012
HTML
<ul id="squares-before">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li>H</li>
<li>I</li>
<li>J</li>
<li>K</li>
<li>L</li>
</ul>
<ul id="squares-after">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li>H</li>
<li>I</li>
<li>J</li>
<li>K</li>
<li>L</li>
</ul>
CSS
#squares-before, #squares-after {
overflow: hidden;
width: 220px;
margin: 0 0 20px 0;
}
#squares-before li, #squares-after li { float: left;
background-color:#333;
margin: 5px;
padding: 10px 0px;
width: 40px;
text-align: center;
color: #fff; }
JavaScript
(function($) {
$.fn.reorder = function(options) {
var order, onStart, onOrdered, onFinished, fadeInDelay;
if (typeof options === 'object') {
for (var prop in options) {
switch (prop) {
case 'order':
order = options.order;
break;
case 'onStart':
onStart = options.onStart
break;
case 'onOrder':
onOrdered = options.onOrder;
break;
case 'onDone':
onFinished = options.onDone;
break;
case 'fadeIn':
fadeInDelay = options.fadein;
break;
}
}
}
else if (typeof options === 'function') {
onFinished = options;
}
// random array sort from
// http://javascript.about.com/library/blsort2.htm
function randOrd() {
return (Math.round(Math.random()) - 0.5);
}
return ($(this).each(function() {
var $this = $(this),
$children = $this.children(),
iEnd = $children.length - 1;
if (typeof order !== 'undefined' && order.length > 0) {
$.each(order, function(j, k) {
$this.append($children.eq(k));
if (j === iEnd && onFinished != undefined) {
onFinished($children.eq(k));
}
else if (typeof onOrdered !== 'undefined') {
onOrdered($children.eq(k));
}
});
}
else {
var indices = [];
for (i = 0; i < $children.length; i++) {
indices[indices.length] = i;
}
indices = indices.sort(randOrd);
$.each(indices,...