mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-01 01:21:04 +08:00
fix: 手机状态栏暗黑模式背景颜色显示不正确
This commit is contained in:
@@ -26,7 +26,28 @@ export default defineNuxtConfig({
|
||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
const theme = mode === 'dark' || mode === 'light' ? mode : (prefersDark ? 'dark' : 'light');
|
||||
document.documentElement.dataset.theme = theme;
|
||||
} catch (e) {}
|
||||
|
||||
|
||||
let themeColor = '#fff';
|
||||
if (theme === 'dark') {
|
||||
themeColor = '#333';
|
||||
} else {
|
||||
themeColor = '#ffffff';
|
||||
}
|
||||
|
||||
const androidMeta = document.createElement('meta');
|
||||
androidMeta.name = 'theme-color';
|
||||
androidMeta.content = themeColor;
|
||||
|
||||
const iosMeta = document.createElement('meta');
|
||||
iosMeta.name = 'apple-mobile-web-app-status-bar-style';
|
||||
iosMeta.content = themeColor;
|
||||
|
||||
document.head.appendChild(androidMeta);
|
||||
document.head.appendChild(iosMeta);
|
||||
} catch (e) {
|
||||
console.warn('Theme initialization failed:', e);
|
||||
}
|
||||
})();
|
||||
`,
|
||||
},
|
||||
|
||||
@@ -24,6 +24,29 @@ function apply(mode) {
|
||||
: mode
|
||||
if (root.dataset.theme === newMode) return
|
||||
root.dataset.theme = newMode
|
||||
|
||||
// 更新 meta 标签
|
||||
const androidMeta = document.querySelector('meta[name="theme-color"]')
|
||||
const iosMeta = document.querySelector('meta[name="apple-mobile-web-app-status-bar-style"]')
|
||||
const themeColor = getComputedStyle(document.documentElement).getPropertyValue('--background-color').trim();
|
||||
|
||||
if (androidMeta) {
|
||||
androidMeta.content = themeColor
|
||||
} else {
|
||||
const newAndroidMeta = document.createElement('meta')
|
||||
newAndroidMeta.name = 'theme-color'
|
||||
newAndroidMeta.content = themeColor
|
||||
document.head.appendChild(newAndroidMeta)
|
||||
}
|
||||
|
||||
if (iosMeta) {
|
||||
iosMeta.content = themeColor
|
||||
} else {
|
||||
const newIosMeta = document.createElement('meta')
|
||||
newIosMeta.name = 'apple-mobile-web-app-status-bar-style'
|
||||
newIosMeta.content = themeColor
|
||||
document.head.appendChild(newIosMeta)
|
||||
}
|
||||
}
|
||||
|
||||
export function initTheme() {
|
||||
|
||||
Reference in New Issue
Block a user