Created
March 11, 2015 20:54
-
-
Save chasemccoy/4beaff5561ddbf7a82a7 to your computer and use it in GitHub Desktop.
Change Background Color of CSS Element via Javascript
This file contains 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
// Add hex colors you want to cycle through in this array | |
var colors = ['#E25243', '#00aeef', '#22d5e5', '#B8E986', '#F5A623', '#4A90E2', '#7F92A8', '#61C037', '#B370EF', '#ffdc29', '#FF6858']; | |
// Calculates the index of the array | |
// Math.random returns a decimal number between 0 and 1 | |
// array.length returns number of items in array | |
var randomColor = Math.floor((Math.random()) * colors.length);var changeBackground = function() { | |
// The element that you want to change needs to have a certain ID, which you can change here | |
document.getElementById("changeColors").style.backgroundColor = colors[randomColor]; | |
}; | |
changeBackground(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment