【问题标题】:How to make a cycle that change the background color every time I click on it?如何制作一个每次点击都会改变背景颜色的循环?
【发布时间】:2020-06-09 04:27:04
【问题描述】:

我正在尝试制作一个静态页面,如果我点击它的任何部分,这个页面就会改变它的颜色。我使用颜色十六进制代码创建了一个对象,但我不知道如何使 document.body.style.background 使用它。

另外,我希望程序能够遵循这种顺序,而不仅仅是抛出随机颜色。对不起,如果我看菜鸟,但我是编程新手。我在自学

如果我这样写就可以了:

function changeBground() {
  var colors;
  document.body.style.background = "#FF0000";
}

但这只是一种颜色,不是我的目标

这是我的完整代码:

document.addEventListener("click", changeBground); 
var colors = {
    red: "#FF0000",
    orange: "#FF7F00",
    yellow: "FFFF00",
    green: "#00FF00",
    aqua: "#00FFFF",
    blue: "#0000FF",
    purple: "#8B00FF",
};

function changeBground()
{
  document.body.style.background = colors;
}

【问题讨论】:

    标签: javascript html css object colors


    【解决方案1】:

    使用Object.keys() 方法获取颜色键,并在每次单击时增加索引,如果索引获得颜色大小,然后将索引设置为 0。请尝试此示例

    document.addEventListener("click", changeBground);
    let index = 0;
    const colors = {
      red: "#FF0000",
      orange: "#FF7F00",
      yellow: "FFFF00",
      green: "#00FF00",
      aqua: "#00FFFF",
      blue: "#0000FF",
      purple: "#8B00FF",
    };
    
    function changeBground() {
      const keys = Object.keys(colors);
    
      document.body.style.backgroundColor = colors[keys[index]];
    
      if (index < keys.length) {
        index += 1;
      } else {
        index = 0;
      }
    }
    

    这里是一个例子

    document.addEventListener("click", changeBground);
    let index = 0;
    const colors = {
      red: "#FF0000",
      orange: "#FF7F00",
      yellow: "FFFF00",
      green: "#00FF00",
      aqua: "#00FFFF",
      blue: "#0000FF",
      purple: "#8B00FF",
    };
    
    function changeBground() {
      const keys = Object.keys(colors);
    
      document.body.style.backgroundColor = colors[keys[index]];
    
      if (index < keys.length) {
        index += 1;
      } else {
        index = 0;
      }
    }

    【讨论】:

    • 太好了,如果你喜欢,我会很感激你选择正确的答案
    【解决方案2】:

    您可以使用Math.random(),生成一个随机数,让您可以从对象中获取随机背景颜色,并结合Object.keys(),这是一个有效的sn-p:

    document.addEventListener("click", changeBground); 
    var colors = {
        red: "#FF0000",
        orange: "#FF7F00",
        yellow: "#FFFF00",
        green: "#00FF00",
        aqua: "#00FFFF",
        blue: "#0000FF",
        purple: "#8B00FF",
    };
    
    const colorKeys = Object.keys(colors);
    
    function getRandomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min)) + min;
    }
    
    function changeBground() {
      const randKey = getRandomInt(0, colorKeys.length);
      console.log(randKey, colorKeys[randKey] );
      const color = colors[colorKeys[randKey]];
      document.body.style.backgroundColor = color;
      
    }

    【讨论】:

    • 感谢您的回复。该代码有效并提供随机颜色,但它跳过了黄色和紫色,这很奇怪
    • @maub98,很高兴为您提供帮助,因为随机数学函数,会检查我是否可以修复它。
    • 已修复,请再次尝试 sn-p,黄色,不包括在内,因为您在对象值中错过了#,我刚刚注意到,对于紫色,数学函数正在返回数组长度的独占值,因此不包括最后一个值,但现在已修复。
    • 哦,是的,我已经看到语法错误了哈哈。是的,它有效!谢谢你,伙计!
    • 很高兴为您提供帮助,如果您有任何有用的答案,请考虑accepting answer :)
    【解决方案3】:

    您正在将颜色对象分配给颜色,这就是为什么这不起作用 - 使用贝洛代码这将起作用。

    document.addEventListener("click", changeBground); 
    var counter = 0;
    var colors = ['red', 'orange', 'yellow', 'green', 'aqua', 'blue', 'purple'];
    
    function changeBground() {
      document.body.style.background = colors[counter];
      counter = counter < colors.length -1 ? counter + 1 : 0;
    }
    

    如果你想随机更新数组中的任何颜色,请使用下面的函数而不是上面的函数 -

    function changeBground() {
      var color = colors[Math.floor(Math.random() * colors.length)];
      document.body.style.background = color;
    }
    

    【讨论】:

    • 谢谢你的回复,伙计!但我仍然不明白关于“将颜色对象分配给颜色”的部分。另外,你能解释一下计数器的计算是如何工作的吗?我从来没有看到使用“?”。我尝试了代码,效果很好,但我不明白为什么哈哈:/
    • @maub98:在您的问题中,您使用了 document.body.style.background = colors;其中颜色是一个对象,而 style.background 是一个颜色的名称或十六进制代码,后跟散列。其次,我使用了“?:”,这被称为三元运算符,因此对于循环颜色更改,我们需要从头到尾遍历颜色的数组,并且计数器帮助我们需要使用哪个索引来获取颜色。在这里,如果索引超过长度,则会导致错误。所以使用“?”是检查条件并相应地更新计数器。希望这可以消除您的疑问。谢谢。
    • 不错!感谢您花时间向我解释:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    相关资源
    最近更新 更多