我正致力于编写屏幕保护程序,使用javaScript进行编码,我希望绘制的线条足够坚固,但是足够透明,你可以看到一个模式出现,因为它继续绘制我&# 39;我有一个随机颜色生成器使用十六进制颜色,我一直试图弄清楚如何设置透明度,使其不是随机的,但其他一切都是。这可能吗?如果是这样的话?
这就是我在颜色生成方面所拥有的......
function getRandomColor()
{
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++)
{
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
答案 0 :(得分:0)
您可以使用rgba而不是hex。
这样的事情:
function getRandomValue(){
for (var i = 0; i < 3; i++)
{
return Math.random() * (255 - 0) + 0; //Don't know how random works
}
}
function getRandomColor()
{
var letters = '0123456789ABCDEF';
var color = 'rgba';
color += `(${getRandomValue()},${getRandomValue()},${getRandomValue()}`;
return color;
}
或尝试在CSS中使用不透明度
答案 1 :(得分:0)
您可以使用rgba(red, green, blue, alpha)
:
function getRandomColor() {
var trans = '0.5'; // 50% transparency
var color = 'rgba(';
for (var i = 0; i < 3; i++) {
color += Math.floor(Math.random() * 255) + ',';
}
color += trans + ')'; // add the transparency
return color;
}
var h1 = document.getElementById('h1');
document.getElementById('cc').onclick=function(){
h1.style.color = getRandomColor();
};
&#13;
<h1 id="h1">Hello, World!</h1>
<button id="cc">Change Color</button>
&#13;