解决暗色模式网页滚动时白色背景

方法就是给html添加一行

1
<meta name="color-scheme" content="dark light">

切换暗色模式的时候:

1
2
3
4
5
6
7
8
9
10
11
12
13
toggleDarkMode() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark;
// 添加 <meta name="color-scheme" content="dark light">
if (this.$vuetify.theme.dark) {
document.querySelector("meta[name='color-scheme']").content = "dark";
// prefers-color-scheme 兼容
document.querySelector("html").setAttribute("data-prefers-color-scheme", "dark");
} else {
document.querySelector("meta[name='color-scheme']").content = "light";
// prefers-color-scheme 兼容
document.querySelector("html").setAttribute("data-prefers-color-scheme", "light");
}
}

效果

bad

good

参考

https://btxx.org/posts/html-dark-mode/

These 3* Lines of CSS Will Give You Dark Mode for Free*

作者

Wei Mo

发布于

2024-02-27

更新于

2024-02-28

许可协议

评论