// define the spreed of the animation in ms
$animationSpeed = 500;
// define the height of the content links in px
$contentHeight = 50;
// define a name by which the elements that should be moved can be indentified
$element = "div.wrapper > a";
// when the page is ready the elements should be given their initial position
$(document).ready(function() {
orderElements(false);
});
/* order the elements as they appear in the HTML source */
function orderElements($animate) {
// make everything extra complicated by having an option to make a smooth animation or not
$animate = $animate === "undefined" ? false : $animate;
// get the amount of elements to animate
$amount = $($element).size();
// give all elements their top margin
for($i = 0; $i < $amount; $i++) {
// define the id of the currently selected element
$id = $i + 1;
// check if a smooth animation should be used and give the element it's initial top margin
if($animate) {
$($element + "#" + $id).animate({marginTop: $i * $contentHeight + "px"}, $animationSpeed);
} else {
$($element + "#" + $id).css("margin-top", $i * $contentHeight);
}
}
}
/* move the elements to a random order */
function shuffleElements() {
// define the amount of elements
$amount = $($element).size();
// define the lowest possible top margin
$topMargin = 0;
// define an array to hold the id's of elements that are done
var done = [0];
// assign a top-margin to every element in a random order
for($i = $amount; $i > 0; $i--) {
// initiate a variable to hold the id of the element
$id = 0;
// keep regenerating a random number until it's not equal to a number in the array
while(jQuery.inArray($id, done) != -1) {
$id = Math.floor((Math.random() * $amount) + 1);
}
// push the new id to the array...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.