window.matchMedia 在 Safari 中不起作用
window.matchMedia does not work in Safari
为什么以下代码在 Safari 中不起作用?
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
console.log('theme changed');
});
如前所述 here on caniuse.com and here on mozilla.org,MediaQueryList.addEventListener
在 Safari 和 IE 中不起作用,但是您可以使用 MediaQueryList.addListener
:
Older browsers should use addListener instead of addEventListener since MediaQueryList only inherits from EventTarget in newer browsers. [source]
为什么以下代码在 Safari 中不起作用?
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
console.log('theme changed');
});
如前所述 here on caniuse.com and here on mozilla.org,MediaQueryList.addEventListener
在 Safari 和 IE 中不起作用,但是您可以使用 MediaQueryList.addListener
:
Older browsers should use addListener instead of addEventListener since MediaQueryList only inherits from EventTarget in newer browsers. [source]