Example One GSAP Code - Use this to make the entire site into dark mode automatically
Example two GSAP code - Use this to have more control over witch elements change into dark mode
Toggle Switch Logic
const toggleCheckbox = document.querySelector("#darkmode-checkbox");
toggleCheckbox.addEventListener("click", () => {
if (!toggleCheckbox.checked) {
// Play Timeline
darkmode.play();
} else {
// Reverse Timeline
darkmode.reverse();
}
});
Toggle Switch HTML
Toggle Switch CSS
selector .switch {
position: relative;
display: inline-block;
width: 50px;
height: 25px;
}
selector .switch input {
opacity: 0;
width: 0;
height: 0;
}
selector .slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
selector .slider:before {
position: absolute;
content: "";
height: 18px;
width: 18px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
selector input:checked + .slider {
background-color: #5149ff;
}
selector input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
selector input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
selector .slider.round {
border-radius: 34px;
}
selector .slider.round:before {
border-radius: 50%;
}
how to save dark mode for other pages ?
Code is updated