mirror of
https://github.com/kognise/water.css.git
synced 2025-12-06 23:05:07 +01:00
Update switch theme script
This commit is contained in:
parent
a0218614e4
commit
7712f6aab9
2 changed files with 36 additions and 4 deletions
38
script.js
38
script.js
|
|
@ -1,4 +1,36 @@
|
|||
document.getElementById('switch').addEventListener('click', function() {
|
||||
(function () {
|
||||
const switchBtn = document.getElementById('switch');
|
||||
const stylesheet = document.getElementById('stylesheet');
|
||||
stylesheet.href = stylesheet.href.replace(/dark|light/, function(replaced) { return replaced === 'dark' ? 'light' : 'dark' });
|
||||
});
|
||||
const mql = matchMedia('(prefers-color-scheme: dark)');
|
||||
const themes = {
|
||||
dark: '../dist/dark.css',
|
||||
darkStandalone: '../dist/dark.standalone.css',
|
||||
light: '../dist/light.css',
|
||||
lightStandalone: '../dist/light.standalone.css'
|
||||
};
|
||||
|
||||
switchBtn.addEventListener('click', function() {
|
||||
switch (stylesheet.getAttribute('href')) {
|
||||
case themes.dark:
|
||||
stylesheet.href = mql.matches ? themes.lightStandalone : themes.darkStandalone;
|
||||
break;
|
||||
case themes.darkStandalone:
|
||||
stylesheet.href = themes.lightStandalone;
|
||||
break;
|
||||
case themes.light || themes.lightStandalone:
|
||||
stylesheet.href = mql.matches ? themes.lightStandalone : themes.darkStandalone;
|
||||
break;
|
||||
case themes.lightStandalone:
|
||||
stylesheet.href = themes.darkStandalone;
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
mql.addListener(function (mql) {
|
||||
if (mql.matches) {
|
||||
stylesheet.href = themes.dark;
|
||||
} else {
|
||||
stylesheet.href = themes.light;
|
||||
}
|
||||
})
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue