Last active
August 29, 2015 14:14
-
-
Save JudeRosario/d49fe4d99f4def773de4 to your computer and use it in GitHub Desktop.
Randomize DOM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Explicitly pass what to randomize to the randomizer function | |
jQuery( document ).ready(function() { | |
jQuery("#div1").randomize("ul", "li.crop-square"); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Logic to randomize stuff from your DOM | |
(function($) { | |
$.fn.randomize = function(tree, childElem) { | |
return this.each(function() { | |
var $this = $(this); | |
if (tree) $this = $(this).find(tree); | |
var unsortedElems = $this.children(childElem); | |
var elems = unsortedElems.clone(); | |
elems.sort(function() { return (Math.round(Math.random())-0.5); }); | |
for(var i=0; i < elems.length; i++) | |
unsortedElems.eq(i).replaceWith(elems[i]); | |
}); | |
}; | |
})(jQuery); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment