[英]Javascript Arrays - How do I select one random hex value?
I have an Array of partial hex values that get random letters appended to them forming a full hex value. 我有一个部分十六进制值的数组,该数组将随机字母附加到它们后形成一个完整的十六进制值。
These are then randomly applied to div layers effectively shading them different colors. 然后将它们随机应用于div图层,以有效地为它们涂上不同的颜色。 However what I get currently is a "Matisse" effect instead of variations of one color.
但是,我目前得到的是“ Matisse”效果,而不是一种颜色的变化。
If you force var color = setHex();
如果您强制
var color = setHex();
to var color = '#CC0';
到
var color = '#CC0';
in the getRandomColor function you will see the effect I am after. 在getRandomColor函数中,您将看到我想要的效果。
I want to know why the "Matisse" effect is happening when I should only be passing one hex value. 我想知道为什么我只应该传递一个十六进制值时会发生“ Matisse”效应。 How do I stop this?
如何停止呢?
See example here: http://jsfiddle.net/fyQhg/ 在此处查看示例: http : //jsfiddle.net/fyQhg/
// Set Hex
function setHex() {
var hexArray = ['#CC0','#FF9','#339'];
var randomHex = hexArray[Math.floor(Math.random() * hexArray.length)];
document.getElementById('inner').innerHTML = randomHex;
return randomHex;
}
// random color
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = setHex();
for (var i = 0; i < 3; i++ ) {
color += letters[Math.round(Math.random() * 7)];
}
return color;
}
//ditribute random colors
function buttonClick() {
var i,j, colorblock = document.getElementsByClassName('shade');
for (i=0, j=colorblock.length; i<j; i++) {
colorblock[i].style.background = getRandomColor();
}
}
window.onload = buttonClick();
var base = setHex();
// random color
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = base;
for (var i = 0; i < 3; i++ ) {
color += letters[Math.round(Math.random() * 7)];
}
return color;
}
Declaring the base outside of the method works. 在方法之外声明基数是可行的。 Example
例
In this method: 在这种方法中:
for (i=0, j=colorblock.length; i<j; i++) { colorblock[i].style.background = getRandomColor(); }
You are calling getRandomColor()
repeatedly in that loop. 您在该循环中反复调用
getRandomColor()
。 Therefore you are also calling setHex()
repeatedly which is creating a new random color each time the loop loops. 因此,您还要重复调用
setHex()
,这会在每次循环时创建新的随机颜色。
So by moving setHex()
outside the method that is inside that loop into base
you are effectively only calling setHex()
once per load. 因此,通过将
setHex()
移到该循环内的方法之外的base
您实际上每次加载仅调用一次setHex()
。
Why? 为什么? Because function
getRandomColor()
calls setHex()
which returns something else every time. 因为函数
getRandomColor()
调用setHex()
,每次都会返回其他内容。
How to stop? 如何停止?
var color = setHex();
for (i=0, j=colorblock.length; i<j; i++) {
colorblock[i].style.background = getRandomColor(color);
}
}
and 和
function getRandomColor(color) {
var letters = '0123456789ABCDEF'.split('');
for (var i = 0; i < 3; i++ ) {
color += letters[Math.round(Math.random() * 7)];
}
return color;
}
Every time you call getRandomColor
you are calling setHex
and picking another random base color. 每次调用
getRandomColor
,都将调用setHex
并选择另一种随机基色。 Just set the base color once, store it in a variable and use it. 只需设置一次基准色,将其存储在变量中并使用即可。
For example: 例如:
var hexBase;
function setHex() {
var hexArray = ['#CC0','#FF9','#339'];
if (!hexBase) {
hexBase = hexArray[Math.floor(Math.random() * hexArray.length)];
}
return hexBase;
}
It seems you were close to this solution because you were storing the result of setHex in a div, but you never checked it again. 似乎您已接近此解决方案,因为您将setHex的结果存储在div中,但是您再也没有检查它。 Also, you shouldn't need to store things in the DOM when you can just store them in JavaScript.
同样,只要将它们存储在JavaScript中,就不需要将其存储在DOM中。
Finally, if you want to avoid the global variable, you can wrap the whole thing in a function (eg your onload
function or a IIFE). 最后,如果要避免使用全局变量,则可以将整个内容包装在一个函数中(例如
onload
函数或IIFE)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.