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>

 

결과보기

 



분류 제목
ScreenOrientation JS - ScreenOrientation.lock() 메서드 - 포함하는 문서의 방향을 기본 방향으로 잠그고…
ScreenOrientation JS - ScreenOrientation.unlock() 메서드 - 포함하는 문서의 방향을 기본 방향에서 잠…
HTML_Objects JS - <tbody> 객체 - 테이블셀그룹 (= 테이블바디 = tbody태그 = 티바디태그) (HTML5수…
HTML_Objects JS - <tfoot> 객체 - 테이블꼬릿말그룹 (= 테이블풋 = tfoot 태그 = 티풋태그) (HTML5…
DOM_Style JS - overflowWrap 속성 - 단어줄바꿈 위해 긴단어쪼개기 (= 긴단어깨기 = 긴단어나누기 = o…
js JS - select() 메서드 - INPUT 텍스트 내용 선택 (= select메서드 = 실렉트메서드|셀렉…
Statement JS - for...of 반복문 - 반복 가능 객체의 각 요소애 대해 반복 실행 (= 포어브반복문)
js JS - 특정 배수만 입력 허용
Functions JS - arguments 객체 (= 아규먼츠객체)
Functions JS - Rest parameter (나머지 매개변수)
String JS - replaceAll() 메서드 - 문자열 모두 대체/변경/변환/치환/바꾸기 (= replaceAll…
js JS - 자동 세미콜론 삽입 (ASI : Automatic semicolon insertion) 적용되는 구…
js JS - JS링크종류 / 이동주소 숨기기 / 팝업창 크기 조절 (= 음악방송링크 창크기 지정) ★ oncli…
DOM_Event JS - returnValue 속성 - (폐기예고) defaultPrevented 속성으로 대체됨. (= r…
js JS - 유튜브 동영상 (전체재생시간/현재재생시간) 체크 ※ youtube iframe (유튜브 아이프레임)…
85/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티
웹유틸
회원센터
홈짱닷컴 PC버전 로그인