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>

 

결과보기

 



분류 제목
DOM_Element JS - dataset 속성 ★★★ - 데이터셋 속성 (= dataset속성) ※ div태그를 iframe태…
js JS - play(), pause() 메서드 - 동영상 재생/멈춤. (= play메서드, pause메서드 =…
Window_Object JS - devicePixelRatio 속성 ★ - 기기 픽셀 비율. ※ PC, 모바일 구별하기
js JS - 입력값 길이 제한 후, 경고창 띄우기
js JS - 눈 내리는 효과 (Snowflakes effect)
js JS - CSS 다크모드 토글 (Dark Mode Toggle) ※ 클래스 상호 대체/변환
DOM_Style JS - pointerEvents 속성 - 마우스 포인트 이벤트에 대한 반응 여부. (= pointerEve…
js JS - 스크롤 정도/위치 진행바 표시기 (Scroll Indicator Bar)
Form JS - input 태그의 name 속성값이 배열일 때, value값 가져오기 (= 폼태그 안 인풋 태그 네…
js JS - 숫자 (카운팅/변동) 애니 효과
DOM_Event JS - orientationchange 이벤트 - 장치 방향 바뀜 이벤트 (= 오리엔테이션체인지 이벤트) …
ScreenOrientation JS - ScreenOrientation 객체 - 장치 방향 정보 갖는 객체 (= 스크린오리엔테이션 객체)
ScreenOrientation JS - ScreenOrientation.type 속성 - 장치 방향 표시 (= 스크린오리엔테이션타입 속성)
ScreenOrientation JS - ScreenOrientation.angle 속성 - 문서의 현재 방향 각도 반환 (= 스크린오리엔테…
ScreenOrientation JS - ScreenOrientation.onchange 속성 ★ - 장치 방향 변경 이벤트 (= 스크린오리…
84/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 1
웹유틸
회원센터
홈짱닷컴 PC버전 로그인