简体   繁体   English

Javascript数组-如何选择一个随机的十六进制值?

[英]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


Explaination 讲解

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; 
} 

Fiddle 小提琴

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.

相关问题 如何在javascript中生成颜色较浅的随机十六进制代码? - How do I generate a random hex code that of a lighter color in javascript? 如何使用Javascript从JSON文件中选择随机对象? - How do I select a random object(?) from a JSON file with Javascript? 如何合并两个数组以在JavaScript中创建一个数组? - How do I merge two arrays to create one array in JavaScript? 如何从 Javascript 中的枚举中获取随机值? - How do I get a random value from enums in Javascript? 如何使随机值不重复? (JavaScript) - How do I make a random value not repeat itself? (JavaScript) 如何将数组与第一个是普通数组,第二个是 JavaScript 中的数组配对? - How do I pair arrays with the first one being a normal array and second one an array of arrays in JavaScript? 我如何通过 JavaScript 中的嵌套 Arrays Map 然后检查嵌套数组的属性之一是否没有值? - How Do I Map through nested Arrays in JavaScript and then check if one of the Nested Array's properties does not have a value? 如何让 Math.random 从 4 个不同的 arrays 中选择相同数量的用户输入值? - How do I make Math.random pick the same amount of user input value from 4 different arrays? 如何在 javascript 中生成随机十六进制字符串 - How to generate random hex string in javascript JavaScript:如何从两个数组中找到最高的随机值得分 - JavaScript: How to find the highest random value score from two arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM