Skip to content

Instantly share code, notes, and snippets.

@akmurray
Created October 1, 2012 13:56
Show Gist options
  • Save akmurray/3811934 to your computer and use it in GitHub Desktop.
Save akmurray/3811934 to your computer and use it in GitHub Desktop.
Javascript required to rotate the css3 logo cube
//NOTE: [element].removeClassName() and [element].addClassName() are not built-in methods, but IMHO they should be.
//Those are defined using unobtrusive javascript if the browser doesn't provide the. See code here: https://github.com/akmurray/aaronkmurray-blog/blob/master/js/prototype-extensions.js
//make a function called "init" that we'll run after the page finishes loading
var init = function() {
///<summary>Code to be run once the page finishes loading html and inline/sync javascript</summary>
///<returns>undefined</returns>
//rotate the logo cube...
var logoRotate = function() {
var cube = document.getElementById('logo-cube'); //get the cube element
for (var i=1;i<=6;i++)
cube.removeClassName('show-side-' + i); //remove the css class for 'showing' this element from all 6 sides
var num = Math.ceil(Math.random()*6); //choose a random side to show
cube.addClassName('show-side-' + num); //add the css class required to show that side
};
//...every few seconds
var intervalLogo = self.setInterval(logoRotate,3000);
};
//tell the browser to run the new init function once it finishes loading the page
if (window.addEventListener) { //new browsers
window.addEventListener('DOMContentLoaded', init, false); //'DOMContentLoaded' - the HTML and JS has been loaded. 'load' - HTML/JS/Images all loaded
} else if (window.attachEvent) { //old IE
window.attachEvent('onload', init);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment