📜  如何使用javascript代码示例获取移动偏好是暗还是亮

📅  最后修改于: 2022-03-11 15:02:12.606000             🧑  作者: Mango

代码示例1
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    // dark mode
}

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
    const newColorScheme = e.matches ? "dark" : "light";
})