修复 elementplus深色模式不能实时变化的问题

This commit is contained in:
2024-10-13 14:18:36 +08:00
parent 0ebcb79259
commit 5c7bd5bb74

View File

@@ -10,5 +10,20 @@
<body> <body>
<div id="app"></div> <div id="app"></div>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
<script>
// 检查用户的主题偏好
const checkTheme = () => {
const htmlElement = document.documentElement;
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
htmlElement.classList.add('dark');
} else {
htmlElement.classList.remove('dark');
}
};
// 监听主题变化
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', checkTheme);
// 初始检查
checkTheme();
</script>
</body> </body>
</html> </html>