Skip to content

Instantly share code, notes, and snippets.

@arlando
Created February 20, 2014 15:41
Show Gist options
  • Save arlando/9116576 to your computer and use it in GitHub Desktop.
Save arlando/9116576 to your computer and use it in GitHub Desktop.
Progress Bar - Decided not to use this because it wasn't as smooth. Good example for the HTML5 Progress Bar API though!
<body>
<progress id="energybar" value="10" max="100"></progress>
<script>
/**
* Created by arlando on 2/20/14.
*/
$(function () {
var style = document.createElement("style"),
sheet = document.head.appendChild(style).sheet,
progressbar = $('#energybar'),
max = progressbar.attr('max'),
value = progressbar.val(),
time = ( 1000 / max ),
rgb = 0,
addValue,
prevrgb,
initialColor = prevrgb = getRandomColor();
function loading() {
value += 1;
addValue = progressbar.val(value);
if (value == max) {
rgb = getRandomColor();
value = 0;
updateSheet(rgb, prevrgb);
prevrgb = rgb;
}
}
var animate = setInterval (function() {
loading();
}, time);
//initialize colors for the sheet
sheet.insertRule('progress::-webkit-progress-bar{ background-color: ' + initialColor + ';}', 0);
sheet.insertRule('progress::-webkit-progress-value{ background-color: ' + initialColor + ';}', 1);
function updateSheet(color, prevcolor) {
sheet.deleteRule(0);
sheet.deleteRule(0);
sheet.insertRule('progress::-webkit-progress-bar{ background-color:' + prevcolor + ';}', 0);
sheet.insertRule('progress::-webkit-progress-value{ background-color:' + color + ';}', 1);
}
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.round(Math.random() * 15)];
}
return color;
}
})();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment