Created
June 6, 2013 06:32
-
-
Save zillou/5719712 to your computer and use it in GitHub Desktop.
A jQuery plugin shuffle DOM elements
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
| (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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment