Last active
May 9, 2020 11:40
-
-
Save kissarat/1f9dff3eb9b0969561bb7bead5aea414 to your computer and use it in GitHub Desktop.
Change order and sort randomly 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Square</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script> | |
<script src="https://code.jquery.com/jquery-3.1.1.js"></script> | |
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" | |
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> | |
<!-- Optional theme --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" | |
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> | |
<!-- Latest compiled and minified JavaScript --> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" | |
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" | |
crossorigin="anonymous"></script> | |
<style type="text/css"> | |
.container { | |
display: flex; | |
margin-left: 0; | |
flex-wrap: wrap; | |
} | |
.container > div { | |
width: 100px; | |
height: 100px; | |
margin: 4px; | |
cursor: pointer; | |
} | |
.container > div:hover { | |
border: 3px dashed black; | |
background-color: transparent; | |
} | |
.red { | |
background-color: red; | |
} | |
.green { | |
background-color: green; | |
} | |
.blue { | |
background-color: blue; | |
} | |
.yellow { | |
background-color: yellow; | |
} | |
.orange { | |
background-color: orange; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<button type="button" id="change-direction">Change direction</button> | |
<button type="button" id="random">Random</button> | |
<button type="button" id="visible">Visible</button> | |
<button type="button" id="add">Add</button> | |
</div> | |
<div class="container"> | |
<div class="red"></div> | |
<div class="green"></div> | |
<div class="blue"></div> | |
<div class="yellow"></div> | |
<div class="orange"></div> | |
</div> | |
<script> | |
const container = $('.container') | |
$('#change-direction') | |
.click(function () { | |
container | |
.css( | |
'flex-direction', | |
'column' === container.css('flex-direction') | |
? 'row' : 'column' | |
) | |
}) | |
$('#random') | |
.click(function () { | |
const children = container.find('div') | |
const range = _.range(0, children.length) | |
console.log('Range: ', range) | |
const random = _.shuffle(range) | |
console.log('Random: ', random) | |
children.each(function (i, el) { | |
el.style.order = random[i] | |
}) | |
}) | |
$('#visible') | |
.click(function () { | |
const container = document.querySelector('.container') | |
if (container.style.display) { | |
container.style.removeProperty('display') | |
} | |
else { | |
container.style.display = 'none' | |
} | |
}) | |
$('#add') | |
.click(function () { | |
const color = Math.floor(Math.random() * 256) | |
const div = $('<div/>').css('background-color', | |
`rgb(${color}, ${256 - color}, ${color})`) | |
container.append(div) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment