Created
February 2, 2011 20:40
-
-
Save mindplay-dk/808383 to your computer and use it in GitHub Desktop.
swap() and jumble() functions for jQuery
This file contains 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
/** | |
* http://plugins.jquery.com/project/swap-jumble | |
*/ | |
(function($){ | |
$.fn.swap = function(b){ | |
b = $(b)[0]; | |
var a = this[0]; | |
var t = a.parentNode.insertBefore(document.createTextNode(''), a); | |
b.parentNode.insertBefore(a, b); | |
t.parentNode.insertBefore(b, t); | |
t.parentNode.removeChild(t); | |
return this; | |
}; | |
$.fn.jumble = function(){ | |
var max = this.length; | |
for (var i=0; i<max*2; i++) { | |
var a = Math.floor(Math.random()*max); | |
var b = a; | |
while (b==a) | |
b = Math.floor(Math.random()*max); | |
$(this[a]).swap(this[b]); | |
} | |
return this; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment