• 회원가입
  • 로그인
  • 구글아이디로 로그인

[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>

 

결과보기

 



분류 제목
js JS - Keyboard KeyCode (키보드키코드 = 키보드키번호 = 키보드키조회 = 키보드코드 = 키보…
js JS - ctrlKey, altKey, shiftKey 속성 - Ctrl키, Alt키, Shift키 눌림여부…
js JS - 숫자만 입력 가능
js JS - 클릭버튼생성 (= 클릭탭생성), 자식요소개별아이디부여, 클릭요소삭제 (= 클릭요소제거 = 클릭부모요…
js JS - F12키차단 (=개발자도구차단) + 마우스오른쪽금지 (= 마우스오른쪽차단 = 불펌방지 = 클릭방지)
js JS - Drag & Drop (= Draggable) - 요소이동 (= 요소끌어이동 = 드래그앤드롭 = 드…
js JS - INPUT입력값을 GET방식으로 넘기기 (=인풋값, 인풋입력밧, 겟방식전달)
HTML_Objects JS - <thead> 객체 - 테이블머리글그룹 (= 테이블헤드그룹 = thead태그 = 티헤드태그) (HT…
js JS - 아이피 유효성검사 (= 아이피 유효성체크 = 아이피 적합성검사 = 아이피 적합성체크)
DOM_Event JS - DOMContentLoaded 이벤트 ★★★★★ - 돔 웹문서 로드 완료 이벤트(= DOMConte…
js JS - Lazyload (레이지로드)
js JS - 스크롤고정 스크롤스파이 메뉴 (= 매뉴얼메뉴 Smooth Scrolling Sticky Scroll…
js JS - 마우스오버 시, 오디오재생 (= mp3재생 = 소리재생)
js JS - 숫자제거 공백제거 정규표현식
js JS - 3D 입체 영상 (= 입체 화면 = 이미지 파노라마 360도 회전 영상 = Image Panoram…
js JS - m3u8 ★ (동영상재생 + 플레이어 다운 + 동영상다운로드 : 엠삼유팔 = 엠쓰리유팔 )
js JS - 이전페이지 보내기 (= 기존페이지로 돌아가기) 3가지방법
js JS - 각종 게임 무료다운로드 주소 좌표 (= 게임다운좌표)
js JS - 서브도메인 입력 후 해당 주소로 이동
js JS - 예약어 (= 변수명・라벨명・함수명 등으로 사용 불가능한 단어)
62/67
목록
찾아주셔서 감사합니다. Since 2012