[英]How to emulate Emulate CSS media feature prefers-color-scheme using JavaScript or CSS?
How to emulate Emulate CSS media feature prefers-color-scheme using JavaScript or CSS, not in Chrome Developer Tools?如何使用 JavaScript 或 CSS 模拟 CSS 媒体功能偏好配色方案,而不是在 Chrome 开发者工具中?
I tried color-scheme: light
and color-scheme: dark
but doesn't work as expected.我尝试color-scheme: light
和color-scheme: dark
但没有按预期工作。
const changeColorSchemeButton = document.querySelector('.change-color-scheme'); const box = document.querySelector('.box'); changeColorSchemeButton.addEventListener('click', function() { if (box.classList.contains('color-scheme-dark')) { box.classList.remove('color-scheme-dark'); box.classList.add('color-scheme-light'); } else { box.classList.add('color-scheme-dark'); box.classList.remove('color-scheme-light'); } })
.box { width: 200px; height: 200px; background: red; color: blue; }.color-scheme-light { color-scheme: light; }.color-scheme-dark { color-scheme: dark; } @media (prefers-color-scheme: dark) {.box { background: grey; color: white; } }
<div class="box"> test </div> <button class="change-color-scheme">Change Color Scheme</button>
As you can see, clicking the button will toggle color-scheme-dark
and color-scheme-light
classes, but the result is the same.如您所见,单击该按钮将切换color-scheme-dark
和color-scheme-light
类,但结果是一样的。
But it's working with Chrome Developers Tool Emulate CSS media feature prefers-color-scheme
feature to simulate dark and light modes truly.但它与 Chrome 开发人员工具Emulate CSS media feature prefers-color-scheme
功能来真正模拟黑暗和光明模式。
How do I truly simulate dark and light modes using JavaScript and CSS?如何使用 JavaScript 和 CSS 真正模拟暗模式和亮模式?
As far as I can read, the color-scheme
property only tells the browser which modes your element can be rendered in a comfortable way.据我所知, color-scheme
属性仅告诉浏览器您的元素可以以哪种模式以舒适的方式呈现。 And depending on that, you allow the browser to override your colors. But it's not guaranteed that this is happening.根据这一点,您允许浏览器覆盖您的 colors。但不能保证会发生这种情况。 You can read more about color-scheme here您可以 在此处阅读有关配色方案的更多信息
But what you can do to automatically set the scheme based on the browser setting and allow the user to switch the mode:但是你可以做什么来根据浏览器设置自动设置方案并允许用户切换模式:
@media (prefers-color-scheme: ...)
只设计你自己的类,不要依赖@media (prefers-color-scheme: ...)
Not sure if this is the best way to be honest, but that's how I do it.不确定这是否是诚实的最佳方式,但我就是这样做的。
In order to detect the users mode you can use window.matchMedia()
, if the media query matches, it's dark mode, otherwise it's light mode.为了检测用户模式,您可以使用window.matchMedia()
,如果媒体查询匹配,则为深色模式,否则为浅色模式。 So one matchMedia is enough.所以一个 matchMedia 就足够了。
const useDark = window.matchMedia("(prefers-color-scheme: dark)");
Then you just set classes for the initial state on your desired element然后你只需在你想要的元素上为初始 state 设置类
<br>
if (useDark.matches) {
box.classList.add('color-scheme-dark');
}
else {
box.classList.add('color-scheme-light');
}
changeColorSchemeButton.addEventListener('click', function() { box.classList.toggle('color-scheme-dark'); box.classList.toggle('color-scheme-light'); });
Full example below下面的完整示例
// detect dark mode const useDark = window.matchMedia("(prefers-color-scheme: dark)"); // the element you want to apply the color scheme, in your case the.box const box = document.querySelector('.box'); // if it matches, then it's dark mode, if it doesn't match, it's light mode // here we set the initial scheme, based on the user setting if (useDark.matches) { box.classList.add('color-scheme-dark'); } else { box.classList.add('color-scheme-light'); } const changeColorSchemeButton = document.querySelector('.change-color-scheme'); // in your click event, you only have to toggle both classes in order to switch between dark and light mode changeColorSchemeButton.addEventListener('click', function() { box.classList.toggle('color-scheme-dark'); box.classList.toggle('color-scheme-light'); });
.box { width: 200px; height: 200px; background: red; color: blue; }.color-scheme-light { background: black; color: white; }.color-scheme-dark { background: gray; color: white; }
<div class="box"> test </div> <button class="change-color-scheme">Change Color Scheme</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.