Skip to content

Instantly share code, notes, and snippets.

@Jazzatola
Created February 1, 2013 17:06
Show Gist options
  • Save Jazzatola/4692623 to your computer and use it in GitHub Desktop.
Save Jazzatola/4692623 to your computer and use it in GitHub Desktop.
Random colours animated with jquery.
<html>
<head>
<title>Random Colors</title>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<style type="text/css">
body {
margin: 0;
}
#canvas {
display: block;
width: 100%;
height: 100%;
background-color: #eeeeee;
text-align: center;
}
#canvas span {
color: #ffffff;
background-color: transparent;
font-family: verdana;
font-size: 5em;
line-height: 5em;
}
</style>
</head>
<body>
<div id="canvas"><span>#eeeeee</span></div>
<script type="text/javascript">
var timer;
function padColor(hexString) {
return '#000000'.substr(0, 7 - hexString.length) + hexString;
};
function randomColor() {
return padColor((~~(Math.random() * (1<<24) )).toString(16));
};
function changeColor() {
var color = randomColor();
$("#canvas").animate({"background-color": color}, "slow", function() {
$(this).find("span").text(color);
console.log(color);
});
}
$(function () {
timer = setInterval(changeColor, 2000);
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment