JavaScript

[js] JS - CSS 다크모드 토글 (Dark Mode Toggle) ※ 클래스 상호 대체/변환

목차

  1. 상태유지 X (방법1, 방법2)
  2. 상태유지 O (방법3, 방법4)

 

상태유지 X (방법1, 방법2)

[방법1] : Light → Dark

 

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

body {

  padding: 25px;

  background-color: white;

  color: black;

  font-size: 25px;

}


.dark-mode {

  background-color: black;

  color: white;

}

</style>


<h2>홈짱닷컴</h2>

<p>Homzzang.com</p>


<button onclick="homzzang()">모드 전환</button>


<script>

function homzzang() {

   var my = document.body;

   my.classList.toggle("dark-mode");

}

</script>

 

결과보기


[방법2] : Dark → Light

 

<style>

body.dark {

  background-color: #111;

  color: #eee;

}

body.dark a {

  color: #111;

}

body.dark button {

  background-color: #eee;

  color: #111;

}


body.light {

  background-color: #eee;

  color: #111;

}

body.light a {

  color: #111;

}

body.light button {

  background-color: #111;

  color: #eee;

}

</style>

 

<body id="body" class="dark">

  <h1>홈짱닷컴</h1>

  <p>Homzzang.com</p>   

  <button type="button" onclick="homzzang()">

 

<script>

function homzzang() {

  var body = document.getElementById("body");

  var currentClass = body.className;

  body.className = (currentClass == "dark") ? "light" : "dark";

}

</script>

 

결과보기

 


상태유지 O (방법3, 방법4)

[방법3] : 상태 유지 O

 

다크 모드 JS 이용

https://github.com/nickdeny/darkmode-js

 


[방법4] : 상태 유지 O - 방법1 업그레이드형

 

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

body {

  padding: 25px;

  background-color: white;

  color: black;

  font-size: 25px;

}


.dark-mode {

  background-color: black;

  color: white;

}

</style>


<body>

<h2>홈짱닷컴</h2>

<p>Homzzang.com</p>


<button type="button" onclick="homzzang()">모드 전환</button>

<div id="theme"></div>

    

<script>

(function() {

  let onpageLoad = localStorage.getItem("theme") || "";

  let element = document.body;

  element.classList.add(onpageLoad);

  document.getElementById("theme").textContent =

    localStorage.getItem("theme") || "light";

})();


function homzzang() {

  let element = document.body;

  element.classList.toggle("dark-mode");


  let theme = localStorage.getItem("theme");

  if (theme && theme === "dark-mode") {

    localStorage.setItem("theme", "");

  } else {

    localStorage.setItem("theme", "dark-mode");

  }


  document.getElementById("theme").textContent = localStorage.getItem("theme");

}

</script>

</body>

 

결과보기

 


방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
Basic JS - Home (JS입문) + Javascript Framework (프레임워크) 종류
Basic JS - Intro (JS소개)
Basic JS - Where To (JS위치) - JS구문 / JS코드위치 / JS사용법 ※ JS외부링크 주의사항
Basic JS - Output (JS출력= JS쓰기) ★★★★★
Basic JS - Syntax (JS구문) ★
Basic JS - Statement (JS구문= JS명령문)
Basic JS - Comment (JS주석)
Basic JS - Variable (JS변수) ★★★★★
Basic JS - Operator (연산자) - JS연산자 ★★★★★
Basic JS - Data Type - 데이터유형 ★★★★★ (= 데이터형식 = 데이터타입 = 데이터종류 = 자료형…
Basic JS - Function - JS함수 ★★★★★ ※ 일반함수 특징 2
Basic JS - Object - JS객체 ★★★★★
Basic JS - Scope - JS유효범위 (= JS접근범위 = 변수 종류) ★★★★★★★★★★
Basic JS - Event - JS이벤트 (= JS코드실행방법) ★★★★★
Basic JS - Strings - JS문자열
1/89
목록
 홈  PC버전 로그인 일본어
웹디자인언어 1
서버관리언어
고급코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 2
웹유틸
회원센터
홈짱 PC버전 로그인