You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
843 B
JavaScript
27 lines
843 B
JavaScript
//https://codepen.io/codeorum/pen/bGedRJO
|
|
|
|
var themeSwitcher = document.querySelector('.theme-switcher input');
|
|
var currentTheme = localStorage.getItem('theme');
|
|
|
|
// check what is current theme right now and activate it
|
|
if (currentTheme) {
|
|
document.documentElement.setAttribute('data-theme', currentTheme);
|
|
if (currentTheme === 'light') {
|
|
themeSwitcher.checked = true;
|
|
}
|
|
}
|
|
|
|
// switch between themes
|
|
function switchTheme(e) {
|
|
if (e.target.checked) {
|
|
document.documentElement.setAttribute('data-theme', 'light');
|
|
localStorage.setItem('theme', 'light');
|
|
}
|
|
else {
|
|
document.documentElement.setAttribute('data-theme', 'dark');
|
|
localStorage.setItem('theme', 'dark');
|
|
}
|
|
}
|
|
|
|
// event listener on checkbox change
|
|
themeSwitcher.addEventListener('change', switchTheme, false); |