Last active
August 29, 2015 13:56
-
-
Save beautifulcode/8985910 to your computer and use it in GitHub Desktop.
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
<html> | |
<head> | |
<style> | |
#wrapper{} | |
.box{float: left; border: 1px solid black;} | |
#main{width: 500px; height: 500px;} | |
.quad-holder{float: left; width: 450px; } | |
.quad{width: 100px; height: 100px; display: block; margin: 20px;} | |
.red{background: red;} | |
.green{background: green; } | |
.blue{background: blue;} | |
.yellow{background: yellow;} | |
.clear{display: block; clear: both;} | |
</style> | |
<script src="http://code.jquery.com/jquery-2.1.0.js"></script> | |
<script> | |
$(document).ready(function(){ | |
function setMain(color){ | |
console.log(color); | |
$('#main').css({backgroundColor: color}); | |
} | |
$('.quad').click(function(e){ | |
var quad = $(e.currentTarget)[0]; | |
var classes = quad.className.split(' '); | |
var colour = classes[classes.length-1]; | |
setMain(colour); | |
}); | |
var colorOrder = []; | |
var quads = $('.quad'); | |
quads.each(function(i, el){ | |
colorOrder[i] = Math.random(quads.length-1); | |
}); | |
var findColor = function(){ | |
var rand_quad = Math.floor(Math.random() * (quads.length - 1 + 1) + 1); | |
var pos = Math.floor(rand_quad); | |
var classes = quads[pos].className.split(' '); | |
var colour = classes[classes.length-1]; | |
setMain(colour) | |
} | |
var timer = window.setInterval(findColor, 500); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="wrapper"> | |
<div id="main" class="box"> | |
</div> | |
<div class="quad-holder"> | |
<div id="box-1" class="box quad red"> | |
</div> | |
<div id="box-2" class="box quad green"> | |
</div> | |
<div class="clear"></div> | |
<div id="box-3" class="box quad blue"> | |
</div> | |
<div id="box-4" class="box quad yellow"> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a more sane version of the same code. Couple helper functions make things much clearer/more dry.