我有一个漂亮的小按钮,它使用很多花哨的CSS看起来不错。
这里是它背后的代码(我现在忽略了兼容性问题);如您所见,它使用一些选择器进行悬停和点击事件。
.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%);
}
但是,为了简化将来会有很多按钮的过程,我想要能够使按钮具有颜色的自定义属性(下面为buttonColor
),这将由一些JavaScript读取,变成了Hue / Saturation / Lightness,并最终改变了许多不同的变化。每个按钮至少包含三种颜色;两个用于渐变,一个用于投影和边框。
<div class="button" id="testButton"buttonColor="ff8c00">
<p class="buttonHeader">foo</p>
<p class="buttonBody">foo2</p>
</div>
以下是我在JavaScript中的内容:
function hexToRgb(hex) { //converts hexadecimal colors into Red/Green/Blue
//code omitted for sake of conciseness
return [r, g, b];
}
function rgbToHsl(r, g, b) { //converts Red/Green/Blue into Hue/Saturation/Lightness
//ditto
return [h, s, l]
}
var buttons = document.body.getElementsByClassName('button'); //Gets all elements with button class
for (var i = 0; i < buttons.length; i++) {
var rgb = hexToRgb(buttons[i].getAttribute("buttoncolor")); //
var hsl = rgbToHsl(rgb.r, rgb.g, rgb.b)
//here
}
就在那里,我被困住了。
我可以轻松修改按钮的样式,但只有在它处于非活动状态时才能修改;我无法改变它在:hover和:active selectors下的反应方式。
答案 0 :(得分:1)
使用数据属性!尝试这样的事情:
<div class="button" id="testButton" data-button-color="ff8c00">
<p class="buttonHeader">foo</p>
<p class="buttonBody">foo2</p>
</div>
JS
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, l];
}
var buttons = document.body.getElementsByClassName('button'); //Gets all elements with button class
for (var i = 0; i < buttons.length; i++) {
var rgb = hexToRgb(buttons[i].data("button-color")),
hsl = rgbToHsl(rgb.r, rgb.g, rgb.b),
rules = [];
rules[i][0] = hsl;
hsl[2] = 100 - hsl[2]; // make second color
rules[i][1] = hsl;
var len = rules.length;
for(;len--;) {
buttons[i].style =
"background: linear-gradient(to bottom, hsla(36, 100%, "+rules[i][0]+"%, 1) 5%, hsla(36, 100%, "+rules[i][1]+"%, 1) 100%);"; // put rules on el
}
}
David Walsh在adding rules to stylesheets with js上发表了一篇优秀文章。
假设你制作了一个规则数组
var rules = [...]; // ['float: left', 'cursor: pointer']
或对象
var rules = {
'hover': [...], // rules...
'active': [...]
};
在上面的代码中。然后,您可以使用以下内容插入它们:
var sheet = (function() {
var style = document.createElement("style");
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
return style.sheet;
})();
function addCSSRule(sheet, selector, rules, index) {
if("insertRule" in sheet) {
sheet.insertRule(selector + "{" + rules + "}", index);
}
else if("addRule" in sheet) {
sheet.addRule(selector, rules, index);
}
}
// ['float: left', 'cursor: pointer']
addCSSRules(document.styleSheets[0], ".button:hover", rules.join(';'));
或
// { 'hover': ['float: left'], 'active': ['cursor: pointer']};
addCSSRules(document.styleSheets[0], ".button:hover", rules.hover.join(';'));
addCSSRules(document.styleSheets[0], ".button:active", rules.active.join(';'));