简体   繁体   English

有没有办法自动改变背景颜色的阴影?

[英]Is there a way to automatically change the shade of background color?

Was wondering if there was a way to use a spectrum to change the background color based off of variables such as temp the higher it is, the more the colour will become orange from blue?想知道是否有一种方法可以使用光谱来根据变量(例如温度)来改变背景颜色,温度越高,颜色就越从蓝色变为橙色? or would I actually have to assign values还是我实际上必须分配值

if (temp < 10) {
document.body.className = "background-cold";
} else if (temp > 10) {
document.body.className = "background-warm";
}
.background-cold {
  background: linear-gradient(
    179.31deg,
    rgba(48, 87, 89, 0.97) 9.28%,
    #fdfeff 167.45%
  );
}

.background-warm {
  background: linear-gradient(
    179.31deg,
    rgba(204, 101, 26, 0.97) 9.28%,
    #fdfeff 167.45%
  );
}
var hue = temp * someScale; // use some formula to determine the appropriate hue from the temperature
var saturation = 1;
var lightness = 1;
document.body.style.backgroundImage = `linear-gradient(179.31deg, hsla(${hue}%,${saturation}%,${lightness}%) 9.28%, #fdfeff 167.45%)`;

For the approach you intend to do I don't recomemend using the .className attribution.对于您打算使用的方法,我不建议使用.className属性。 It will be hardcoded and that's not what you want.它将被硬编码,这不是你想要的。 You can just use style.setProperty(string, string) in wich the first parameter is the CSS atttribute and the second parameter is the CSS value .您可以只使用style.setProperty(string, string) ,其中第一个参数是CSS 属性,第二个参数是CSS 值 Both of them need to be strings in order to work.它们都需要是字符串才能工作。

let colorValue = 'linear-gradient(VALUES)';
document.body.style.setProperty('background', colorValue);

By using the previous code you can now set directly the background you want.通过使用前面的代码,您现在可以直接设置所需的背景。 However your still need a function to create a dynamic colorValue according to the temperature .但是,您仍然需要 function 来根据温度创建动态colorValue This can become a little complex depending on the color values you intend to apply.根据您打算应用的颜色值,这可能会变得有点复杂。 A possible solution for this can be found in the following link: https://stackoverflow.com/a/12934900/11860800 .可以在以下链接中找到可能的解决方案: https://stackoverflow.com/a/12934900/11860800

I hope this answer helps you!我希望这个答案对你有帮助! Happy coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法自动更改特定文本颜色以适合背景颜色? - Is there a way to automatically change a specific text color so it fits the background color? 有没有一种方法可以在带有扩展程序的Chrome上为标签页着色/上色/突出显示? - Is there a way to shade/color/highlight a tab on Chrome with an extension? 有没有办法改变dygraph标签的背景颜色? - Is there a way to change background-color of dygraph labels? 更改 toastr 和 sweetalert 背景颜色的简单方法 - Easy way to change toastr and sweetalert background color 1秒后随机自动更改背景颜色(Javascript) - Change background color randomly and automatically after 1 second (Javascript) 有没有办法根据 CSS 中另一个元素的背景颜色来更改文本的颜色? - is there a way to change the color of the text depending on the background color of another element in CSS? 字体颜色在某个阴影以下不会改变 - Font color won't change below a certain shade 根据元素的属性更改背景颜色的更好方法 - Better way to change background color based on element's property 使用JS更改页面背景颜色的最快方法? - Fastest way to change page background color using JS? 使用JavaScript HTML DOM更改背景颜色的最佳方法 - Best way to change background color with JavaScript HTML DOM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM