How do I detect dark mode using JavaScript?

后端 未结 3 1124
庸人自扰
庸人自扰 2020-11-28 23:55

Windows and macOS now have dark mode.

For CSS I can use:

    @media (prefers-dark-interface) { 
      color:          


        
3条回答
  •  鱼传尺愫
    2020-11-29 00:35

    According to MediaQueryList - Web APIs | MDN, addListener is the correct way to listen to the change. addEventListener is not working for me on iOS 13.4.

    window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
      console.log(`changed to ${e.matches ? "dark" : "light"} mode`)
    });
    

提交回复
热议问题