如何使用 JavaScript 检测暗模式?
How do I detect dark mode using JavaScript?
Windows 和 macOS 现在有深色模式。
对于CSS我可以使用:
@media (prefers-dark-interface) {
color: white; background: black
}
但是我正在使用the Stripe Elements API, which puts colors in JavaScript
例如:
const stripeElementStyles = {
base: {
color: COLORS.darkGrey,
fontFamily: `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"`,
fontSize: '18px',
fontSmoothing: 'antialiased',
'::placeholder': {
color: COLORS.midgrey
},
':-webkit-autofill': {
color: COLORS.icyWhite
}
}
}
如何在 JavaScript 中检测 OS 的首选配色方案?
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
// dark mode
}
关注变化:
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
const newColorScheme = event.matches ? "dark" : "light";
});
您可以直接用JavaScript
检查CSSMedia-Queries
The window.matchMedia() method returns a MediaQueryList object
representing the results of the specified CSS media query string. The
value of the matchMedia() method can be any of the media features of
the CSS @media rule, like min-height, min-width, orientation, etc.
要检查媒体查询是否为真,可以使用matches
属性
// Check to see if Media-Queries are supported
if (window.matchMedia) {
// Check if the dark-mode Media-Query matches
if(window.matchMedia('(prefers-color-scheme: dark)').matches){
// Dark
} else {
// Light
}
} else {
// Default (when Media-Queries are not supported)
}
要根据用户的偏好动态更新 color-scheme
,可以使用以下内容:
function setColorScheme(scheme) {
switch(scheme){
case 'dark':
// Dark
break;
case 'light':
// Light
break;
default:
// Default
break;
}
}
function getPreferredColorScheme() {
if (window.matchMedia) {
if(window.matchMedia('(prefers-color-scheme: dark)').matches){
return 'dark';
} else {
return 'light';
}
}
return 'light';
}
if(window.matchMedia){
var colorSchemeQuery = window.matchMedia('(prefers-color-scheme: dark)');
colorSchemeQuery.addEventListener('change', setColorScheme(getPreferredColorScheme()));
}
根据MediaQueryList - Web APIs | MDN,addListener
是听变化的正确方法。 addEventListener
在 iOS 13.4.
上对我不起作用
window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
console.log(`changed to ${e.matches ? "dark" : "light"} mode`)
});
如果您使用 nuxt 开发您的应用程序,您需要 color-mode。
Windows 和 macOS 现在有深色模式。
对于CSS我可以使用:
@media (prefers-dark-interface) {
color: white; background: black
}
但是我正在使用the Stripe Elements API, which puts colors in JavaScript
例如:
const stripeElementStyles = {
base: {
color: COLORS.darkGrey,
fontFamily: `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"`,
fontSize: '18px',
fontSmoothing: 'antialiased',
'::placeholder': {
color: COLORS.midgrey
},
':-webkit-autofill': {
color: COLORS.icyWhite
}
}
}
如何在 JavaScript 中检测 OS 的首选配色方案?
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
// dark mode
}
关注变化:
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
const newColorScheme = event.matches ? "dark" : "light";
});
您可以直接用JavaScript
检查CSSMedia-QueriesThe window.matchMedia() method returns a MediaQueryList object representing the results of the specified CSS media query string. The value of the matchMedia() method can be any of the media features of the CSS @media rule, like min-height, min-width, orientation, etc.
要检查媒体查询是否为真,可以使用matches
属性
// Check to see if Media-Queries are supported
if (window.matchMedia) {
// Check if the dark-mode Media-Query matches
if(window.matchMedia('(prefers-color-scheme: dark)').matches){
// Dark
} else {
// Light
}
} else {
// Default (when Media-Queries are not supported)
}
要根据用户的偏好动态更新 color-scheme
,可以使用以下内容:
function setColorScheme(scheme) {
switch(scheme){
case 'dark':
// Dark
break;
case 'light':
// Light
break;
default:
// Default
break;
}
}
function getPreferredColorScheme() {
if (window.matchMedia) {
if(window.matchMedia('(prefers-color-scheme: dark)').matches){
return 'dark';
} else {
return 'light';
}
}
return 'light';
}
if(window.matchMedia){
var colorSchemeQuery = window.matchMedia('(prefers-color-scheme: dark)');
colorSchemeQuery.addEventListener('change', setColorScheme(getPreferredColorScheme()));
}
根据MediaQueryList - Web APIs | MDN,addListener
是听变化的正确方法。 addEventListener
在 iOS 13.4.
window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
console.log(`changed to ${e.matches ? "dark" : "light"} mode`)
});
如果您使用 nuxt 开发您的应用程序,您需要 color-mode。