gpt4 book ai didi

javascript - 如何覆盖css偏好颜色方案设置

转载 作者:行者123 更新时间:2023-12-01 11:34:35 25 4
gpt4 key购买 nike

我正在实现暗模式,因为 macOS、Windows 和 iOS 都引入了暗模式。
Safari、Chrome 和 Firefox 有一个原生选项,使用以下 CSS 媒体规则:

@media (prefers-color-scheme: dark) {
body {
color:#fff;
background:#333333
}
这将自动识别设置为暗模式的系统,并应用随附的 CSS 规则。
然而;即使用户可能将他们的系统设置为暗模式,他们也可能更喜欢特定网站的浅色或默认主题。还有一种情况是 Microsoft Edge 用户(尚)不支持 @media (prefers-color-scheme .为了获得最佳用户体验,我想确保这些用户可以在这些情况下在暗模式和默认模式之间切换。
是否有可以执行此操作的方法,可能使用 HTML 5 或 JavaScript?我会包含我尝试过的代码,但我无法找到任何关于实现它的信息!

最佳答案

我已经确定了一个合适的解决方案,如下:

CSS 将使用变量和主题:

// root/default variables
:root {
--font-color: #000;
--link-color:#1C75B9;
--link-white-color:#fff;
--bg-color: rgb(243,243,243);
}
//dark theme
[data-theme="dark"] {
--font-color: #c1bfbd;
--link-color:#0a86da;
--link-white-color:#c1bfbd;
--bg-color: #333;
}

然后在必要时调用变量,例如:
//the redundancy is for backwards compatibility with browsers that do not support CSS variables.
body
{
color:#000;
color:var(--font-color);
background:rgb(243,243,243);
background:var(--bg-color);
}

JavaScript 用于识别用户设置了哪个主题,或者他们是否覆盖了他们的操作系统主题,以及在两者之间切换,这包含在 html <body>...</body> 输出之前的标题中:
//determines if the user has a set theme
function detectColorScheme(){
var theme="light"; //default to light

//local storage is used to override OS theme settings
if(localStorage.getItem("theme")){
if(localStorage.getItem("theme") == "dark"){
var theme = "dark";
}
} else if(!window.matchMedia) {
//matchMedia method not supported
return false;
} else if(window.matchMedia("(prefers-color-scheme: dark)").matches) {
//OS theme setting detected as dark
var theme = "dark";
}

//dark theme preferred, set document with a `data-theme` attribute
if (theme=="dark") {
document.documentElement.setAttribute("data-theme", "dark");
}
}
detectColorScheme();

此javascript用于在设置之间切换,不需要包含在页面标题中,但可以包含在任何地方
//identify the toggle switch HTML element
const toggleSwitch = document.querySelector('#theme-switch input[type="checkbox"]');

//function that changes the theme, and sets a localStorage variable to track the theme between page loads
function switchTheme(e) {
if (e.target.checked) {
localStorage.setItem('theme', 'dark');
document.documentElement.setAttribute('data-theme', 'dark');
toggleSwitch.checked = true;
} else {
localStorage.setItem('theme', 'light');
document.documentElement.setAttribute('data-theme', 'light');
toggleSwitch.checked = false;
}
}

//listener for changing themes
toggleSwitch.addEventListener('change', switchTheme, false);

//pre-check the dark-theme checkbox if dark-theme is set
if (document.documentElement.getAttribute("data-theme") == "dark"){
toggleSwitch.checked = true;
}

最后,在主题之间切换的 HTML 复选框:
<label id="theme-switch" class="theme-switch" for="checkbox_theme">
<input type="checkbox" id="checkbox_theme">
</label>

通过使用 CSS 变量和 JavaScript,我们可以自动确定用户主题,应用它,并允许用户覆盖它。 [截至目前(2019/06/10),只有 Firefox 和 Safari 支持自动主题检测]

关于javascript - 如何覆盖css偏好颜色方案设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56300132/

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