I have created a stylish button using some fancy CSS techniques.
Below is the CSS code for the button, including hover and click events:
.button {
display: inline-block;
width: 200px;
height: 200px;
padding: 15px;
border-radius: 25px;
background:linear-gradient(to bottom, hsla(36, 100%, 60%, 1) 5%, hsla(36, 100%, 40%, 1) 100%);
border:2px solid hsla(36, 100%, 30%, 1);
box-shadow:inset 0px 2px 2px 0px white;
position: relative;
left: 0px;
top: 0px;
text-shadow:0px 1px 0px hsla(36, 100%, 30%, 1);
margin: 25px;
}
.button:hover {
background:linear-gradient(to bottom, hsla(36, 100%, 65%, 1) 5%, hsla(36, 100%, 45%, 1) 100%);
}
.button:active {
background:linear-gradient(to bottom, hsla(36, 100%, 40%, 1) 5%, hsla(36, 100%, 60%, 1) 100%);
}
To create multiple variations of this button efficiently in the future, I am exploring the idea of adding a custom attribute called buttonColor
. This attribute will be read by JavaScript to dynamically adjust the colors of the button. Each button will have at least three colors; two for the gradient and one for the drop shadow and border.
<div class="button" id="testButton" buttonColor="ff8c00">
<p class="buttonHeader">foo</p>
<p class="buttonBody">foo2</p>
</div>
Here's where I need help with the JavaScript logic:
function hexToRgb(hex) {
// Omitted code for brevity
return [r, g, b];
}
function rgbToHsl(r, g, b) {
// Omitted code for brevity
return [h, s, l]
}
var buttons = document.body.getElementsByClassName('button');
for (var i = 0; i < buttons.length; i++) {
var rgb = hexToRgb(buttons[i].getAttribute("buttoncolor"));
var hsl = rgbToHsl(rgb.r, rgb.g, rgb.b)
// Need assistance here
}
I can update the button style easily, but I'm struggling with modifying its appearance during :hover and :active states.