class-website/function.js

35 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-04-12 16:48:44 +02:00
var checkbox = document.querySelector('input[name=mode]');
checkbox.addEventListener('change', function() {
if(this.checked) {
trans()
document.documentElement.setAttribute('data-theme', 'dartheme')
} else {
trans()
document.documentElement.setAttribute('data-theme', 'lighttheme')
}
})
let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() => {
document.documentElement.classList.remove('transition');
}, 1000)
2022-04-12 17:35:40 +02:00
}
const btn = document.querySelector(".container_toggle");
const currentTheme = localStorage.getItem("theme");
if (currentTheme == "dark") {
document.body.classList.add("dark-theme");
}
btn.addEventListener("click", function () {
document.body.classList.toggle("dark-theme");
let theme = "light";
if (document.body.classList.contains("dark-theme")) {
theme = "dark";
}
localStorage.setItem("theme", theme);
});