본문 바로가기
Js

다크모드 로컬 저장

by 영감은어디에 2024. 6. 30.

<div class="c-mode"></div>
const body = document.querySelector("body"),
  cMode = document.querySelector(".c-mode");
let getMode = localStorage.getItem("mode");
if (getMode && getMode == "dark") {
  cMode.classList.add("dark");
  body.classList.add("dark");
}
cMode.addEventListener("click", () => {
  cMode.classList.toggle("dark");
  body.classList.toggle("dark");
  if (!body.classList.contains("dark")) {
    return localStorage.setItem("mode", "light");
  }
  localStorage.setItem("mode", "dark");
});