Created
October 19, 2017 22:53
-
-
Save lmccart/084161a66f58a9925936728d81fdd6a2 to your computer and use it in GitHub Desktop.
random order
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>jQuery demo</title> | |
<link rel="stylesheet" href="style.css"></link> | |
<script src="jquery.js"></script> | |
<script src="main.js"></script> | |
</head> | |
<body> | |
<!-- delete any width and height info from url --> | |
<div class='random'>item 1</div> | |
<div class='random'>item 2</div> | |
<div class='random'>item 3</div> | |
<div class='random'>item 4</div> | |
</body> | |
</html> |
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
$(document).ready(function() { | |
function shuffle(array) { | |
var currentIndex = array.length, temporaryValue, randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
var elements = $('.random'); | |
elements.remove(); | |
shuffle(elements); | |
for (var i=0; i < elements.length; i++) { | |
$('body').append(elements[i]); | |
} | |
}); |
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
.random:nth-child(1) { | |
position: absolute; | |
left: 100px; | |
top: 200px; | |
} | |
.random:nth-child(2) { | |
position: absolute; | |
left: 200px; | |
top: 500px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment