简体   繁体   English

如何覆盖 CSS 配色方案设置

[英]How to overwrite CSS color scheme settings

In HTML.在 HTML 中。 Now I know we can ask CSS to do something when user change their system color scheme, like现在我知道我们可以要求 CSS 在用户更改系统配色方案时做一些事情,比如

@media (prefers-color-scheme: dark)

But what if I want to overwrite that setting like I need light color scheme at night, but I don't want to change my system color scheme settings, is there any JavaScript or jQuery way to fool CSS (to proceed the dark park even when system is light, or opposite ), so I can switch webpage color scheme by click some element?但是如果我想覆盖那个设置,比如我需要在晚上使用浅色方案,但我不想更改我的系统配色方案设置,有没有 JavaScript 或 jQuery 方法来愚弄 Z2C56C360580420D293172FZED系统很轻,或者相反),所以我可以通过点击一些元素来切换网页配色方案?

You can define two classes for it.您可以为它定义两个类。 Eg.例如。 "colourSchemeDay" and "colourSchemeNight" in your CSS file. CSS 文件中的“colourSchemeDay”和“colourSchemeNight”。 Then, you can manipulate the classes by having a time constraint on them.然后,您可以通过对它们进行时间限制来操作这些类。

if (time >=6AM && time <= 8PM){
    document.getElementById("myElement");
    element.classList.remove("colourSchemeNight");
    element.classList.add ("colourSchemeDay");
} else {
    document.getElementById("myElement");
    element.classList.add("colourSchemeNight");
    element.classList.remove("colourSchemeDay");
}

You should first get the current time in 24 hour format like so:您应该首先以 24 小时格式获取当前时间,如下所示:

var dayNight = (new Date()).getHours();

Now, you most probably have 2 classes with background color, 1 for day and 1 for night.现在,您很可能有 2 个具有背景颜色的类,1 个用于白天,1 个用于夜晚。

So, you can use if/else statement to decide when you want to add or remove a class using JQuery.因此,您可以使用if/else语句来决定何时使用 JQuery 添加或删除 class。

So in my example, when the .dayNight is bigger or equals to 7 (7am) and lesser than 19 (7pm), it adds the .day class and removes the.因此,在我的示例中,当.dayNight大于或等于 7(早上 7 点)且小于 19(晚上 7 点)时,它会添加.day class 并删除。 night class. night class。

And when the .dayNight is bigger or equals to 0 (12 midnight) AND if it is smaller than 7 (7am), it adds the .night class and removes the .day class..dayNight大于或等于 0(午夜 12 点)且小于 7(早上 7 点)时,它会添加.night class 并删除.day class。

Also, when it is bigger or equals to 19 (7pm) and equals or lesser than 23 (11pm) it does the same above.此外,当它大于或等于 19(晚上 7 点)且等于或小于 23(晚上 11 点)时,它执行相同的操作。 We've already assigned 0 (12 midnight) in the above code, so we can set it to 23 (11pm) here.我们已经在上面的代码中指定了 0(午夜 12 点),所以我们可以在这里将其设置为 23(晚上 11 点)。

Try this:尝试这个:

 var dayNight = (new Date()).getHours(); if(dayNight>=7 && dayNight < 19) { $( "#display" ).removeClass( "night" ).addClass( "day" ); } if((dayNight>=0 && dayNight<7) || (dayNight>=19 && dayNight <=23)) { $( "#display" ).removeClass( "day" ).addClass( "night" ); }
 .day { background-color: blue; color: white; }.night { background-color: red; color: white; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="display"> Hello! </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM