admin管理员组文章数量:1418675
I am trying to create more or less realistic looking leds with HTML and CSS. Adding a a glow to every led would be pretty straight forward if the color of the led was static. But I actually want the glow to automatically adjust to the led color. So this wouldn't work: / And please don't tell me to create a class for every color. In the end I want the leds to fade between colors, so I would have to create millions of classes.
.led {
width: 10px;
height: 10px;
border-radius: 50%;
box-shadow: 0px 0px 6px 2px #ff0000;
float:left;
margin-right: 10px;
}
<div class="led" style="background-color:red"></div>
<div class="led" style="background-color:green"></div>
<div class="led" style="background-color:blue"></div>
I am trying to create more or less realistic looking leds with HTML and CSS. Adding a a glow to every led would be pretty straight forward if the color of the led was static. But I actually want the glow to automatically adjust to the led color. So this wouldn't work: https://fiddle.jshell/dwv5xxws/ And please don't tell me to create a class for every color. In the end I want the leds to fade between colors, so I would have to create millions of classes.
.led {
width: 10px;
height: 10px;
border-radius: 50%;
box-shadow: 0px 0px 6px 2px #ff0000;
float:left;
margin-right: 10px;
}
<div class="led" style="background-color:red"></div>
<div class="led" style="background-color:green"></div>
<div class="led" style="background-color:blue"></div>
This is what inspired me btw: http://codepen.io/fskirschbaum/pen/MYJNaj
edit: Just an idea.. maybe we can just increase the size of the leds and make a shadow overlay inside of those leds so that it appears like the outer circle is just the "glow" and not the led itself.
Share Improve this question edited Jul 14, 2016 at 18:39 Forivin asked Jul 14, 2016 at 18:31 ForivinForivin 15.6k30 gold badges118 silver badges212 bronze badges 9- Why not just make a different CSS class for each LED? – Zac Commented Jul 14, 2016 at 18:34
- is given codepen example ok? fiddle.jshell/dwv5xxws/2 – Madhawa Priyashantha Commented Jul 14, 2016 at 18:35
- @Zac Because I'm planning to make the leds fade through every possible color from #000000 to #ffffff. And I don't feel like creating 16777216 classes right now. – Forivin Commented Jul 14, 2016 at 18:36
- @FastSnail Well, no. When the background color of the led is changed, I want the glow to automatically look correct. – Forivin Commented Jul 14, 2016 at 18:37
- 2 @Forivin canvas is more suitable – Madhawa Priyashantha Commented Jul 14, 2016 at 18:45
3 Answers
Reset to default 3CSS Variables are still at a very early stage, but one well-established, reliable variable in CSS is currentColor
- which, as its name suggests, always takes the current value of whatever color:
is.
So a slight tweak to your code above will give you exactly what you want:
.led {
width: 10px;
height: 10px;
background-color: currentColor;
border-radius: 50%;
box-shadow: 0px 0px 6px 2px currentColor;
float:left;
margin-right: 10px;
}
<div class="led" style="color:red"></div>
<div class="led" style="color:green"></div>
<div class="led" style="color:blue"></div>
For browser support of currentColor
, see: http://caniuse./#feat=currentcolor
Okay, so I used JS to iterate all the elements with the class 'led', then set their box shadows to their background colours:
leds = document.getElementsByClassName('led');
for (var l = 0; l < leds.length; l++) {
led = leds[l];
led.style.boxShadow = '0 0 10px 1px ' + led.style.backgroundColor;
}
If you change the background colour of an element, make sure to run this code again.
It is your idea, after all ...
.led {
width: 20px;
height: 20px;
border-radius: 50%;
box-shadow: inset 0px 0px 11px 3px rgba(255,255,255,0.8);
float:left;
margin-right: 10px;
}
<div class="led" style="background-color:red"></div>
<div class="led" style="background-color:green"></div>
<div class="led" style="background-color:blue"></div>
本文标签: javascriptCss Glow effect that takes background color into accountStack Overflow
版权声明:本文标题:javascript - Css: Glow effect that takes background color into account? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745281249a2651435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论