gpt4 book ai didi

javascript - JS 颜色生成器不工作

转载 作者:行者123 更新时间:2023-12-03 11:00:15 25 4
gpt4 key购买 nike

我正在尝试制作一个网页,其中 JS 将生成两种颜色并在渐变中使用它们。这是我所拥有的:

HTML

<div id="hi"></div>

CSS

div {
width:100px;
height:100px;
}

JS

var hue = Math.floor(Math.random() * 360);
var top = "hsl(" + hue + ",100%,30%";
var bottom = "hsl(" + hue + ",100%,50%)";
document.getElementById('hi').style.background = "linear-gradient(" + top + "," + bottom + ")";

演示不起作用:http://jsfiddle.net/8pzaeo3w/16/

最佳答案

来自MDN documentation :

Gradients are defined as CSS <image> data types; they can be used only where an image data type is required. For this reason, linear-gradient won't work on background-color and other properties requesting <color>.

但是,您可以将其分配给 background 。另一个问题是你的top值(value)。您产生的值(value)是:

hsl(286100%,30%

我相信你能找出问题所在。

如果你解决了这两个问题,它就会起作用。我推荐给learn how to debug JavaScript这样您就可以自己检查变量。

关于javascript - JS 颜色生成器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28132075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com