๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’› Frontend/๐Ÿ’› Frontend : JavaScript

[Javascript] ๋ฒ„ํŠผ ํ•˜๋‚˜๋กœ ๋‹คํฌ ๋ชจ๋“œ ์„ค์ •ํ•˜๊ธฐ (toggle)

by eyes from es 2025. 2. 3.
728x90
๋ฐ˜์‘ํ˜•

 

 

์˜ค๋Š˜ ํ•ด ๋ณผ ์‹ค์Šต์ž…๋‹ˆ๋‹ค.

๋ผ์ดํŠธ ๋ชจ๋“œ vs ๋‹คํฌ ๋ชจ๋“œ

 

 

โœ… ๋ชฉํ‘œ


  • ๋ฒ„ํŠผ ํ•˜๋‚˜๋ฅผ ํด๋ฆญํ•˜๋ฉด์„œ ๋‹คํฌ๋ชจ๋“œ, ๋ผ์ดํŠธ๋ชจ๋“œ๋ฅผ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹คํฌ๋ชจ๋“œ, ๋ผ์ดํŠธ๋ชจ๋“œ์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ ์‹œ toggle์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

 

๋จผ์ €, html ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

์ด ์‹ค์Šต์€ ๋‹คํฌ๋ชจ๋“œ, ๋ผ์ดํŠธ๋ณ€ํ™˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‹ค์Šต์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ˆ html์€ ๊ฐ„๋‹จํžˆ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 


โœ… Html


  • ๊ฐ€์šด๋ฐ ์ค‘์•™ ์ •๋ ฌ๋กœ div ๋ฐ•์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— ๋ฒ„ํŠผ์„ ๊ณ ์ •ํ•ฉ๋‹ˆ๋‹ค.
<body class="wrap light-mode">

  <div>
    <h1>Web Programming</h1>
    <ul>
      <li>HTML</li>
      <li>CSS</li>
      <li>Javascipt</li>
    </ul>
  </div>

  <button id="modeButton">
    ๋‹คํฌ ๋ชจ๋“œ
  </button>
  
</body>

 

  • body ์ „์ฒด์— ํด๋ž˜์Šค๋ฅผ ์ฃผ์–ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค body ์ „์ฒด์˜ ๋ฐฐ๊ฒฝ์ƒ‰, ๊ธ€์ž ์ƒ‰์„ ํ•œ๋ฒˆ์— ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
  • body ํด๋ž˜์Šค์— light-mode ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆด ๋•Œ dark-mode๋กœ ํด๋ž˜์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด dark-mode CSS ๊ฐ€ ์ ์šฉ๋˜๊ณ 
    • light-mode๋กœ ํด๋ž˜์Šค๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด light-mode CSS๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. 
      • ์ด๋•Œ, dark-mode ์ธ ์ƒํ™ฉ์—์„œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด light-mode๋กœ ๋ณ€๊ฒฝ๋˜๊ณ 
      • light-mode์ธ ์ƒํ™ฉ์—์„œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด dark-mode๋กœ ๋ณ€๊ฒฝ๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด
      • toggle์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

โœ… CSS


<style>
    *{
      box-sizing: border-box;
    }

    body{
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    button{
      position: fixed;
      top: 20px;
      right: 20px;
      padding: 10px;
      background-color: white;
      color: black;
    }

    .dark-mode{
      background-color: black;
      color: white;
    }

    .dark-mode button{
      background-color: black;
      border: 1px solid white;
      color: white;
    }

    .light-mode{
      background-color: white;
      color: black;
    }

  </style>

 

  • ๋ชจ๋“œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ฒ„ํŠผ ๋‚ด๋ถ€ ์ƒ‰๊ณผ ๊ธ€์ž ์ƒ‰๋„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋ฏ€๋กœ 
  • .dark-mode button
  • .dark-mode ํด๋ž˜์Šค ์•ˆ์— ์žˆ๋Š” button ์œผ๋กœ CSS ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

 

โœ… JavaScript


  <script>

    document.addEventListener('DOMContentLoaded', function(){
      const modeBtn = document.querySelector('#modeButton');
      const body= document.body;

      modeBtn.addEventListener('click', (e)=>{
        body.classList.toggle('dark-mode');
        body.classList.toggle('light-mode');

        if(body.classList.contains('dark-mode')){
          modeBtn.innerText = "๋‹คํฌ ๋ชจ๋“œ";
        }else{
          modeBtn.innerText = "๋ผ์ดํŠธ ๋ชจ๋“œ";
        }

      });
    });

  </script>

 

DomContentLoaded : 

๋ฌธ์„œ ๋‚ด์šฉ์ด ๋ชจ๋‘ ๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰ํ•˜์—ฌ, ๋„คํŠธ์›Œํฌ ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ๋•Œ์— ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” CSS ์˜ค๋ฅ˜๋ฅผ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ’ก ํด๋ž˜์Šค ์†์„ฑ toggle ํ•˜๊ธฐ ๐Ÿ’ก

toggle์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

document.addEventListener('DOMContentLoaded', function(){
      const modeBtn = document.querySelector('#modeButton');
      const body= document.body;

      modeBtn.addEventListener('click', (e)=>{
        // body.classList.toggle('dark-mode');
        // body.classList.toggle('light-mode');

        if(body.classList.contains('dark-mode')){
          modeBtn.innerText = "๋‹คํฌ ๋ชจ๋“œ";
          body.classList.remove('dark-mode');
          body.classList.add('light-mode');
        }else{
          modeBtn.innerText = "๋ผ์ดํŠธ ๋ชจ๋“œ";
          body.classList.add('dark-mode');
          body.classList.remove('light-mode');
        }

      });
    });

 

ํ•˜์ง€๋งŒ remove์™€ add๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๋‹ค์‹œ ์ถ”๊ฐ€ํ•˜๋Š” ๊ณผ์ •์ด ๊ฝค ๋ฒˆ๊ฑฐ๋กญ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ์ด toggle ์ž…๋‹ˆ๋‹ค.

 

toggle์ด๋ž€,  ์˜จ/์˜คํ”„, ๋„๊ธฐ/์ผœ๊ธฐ ์ฒ˜๋Ÿผ 2๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ๋ฒˆ๊ฐˆ์•„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

 

toggle ํ•จ์ˆ˜๋Š” ์ด ์‹ค์Šต์ฒ˜๋Ÿผ

  • ํด๋ž˜์Šค ์ด๋ฆ„๋งŒ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ ,
  • ์กฐ๊ฑด์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์ด true์ธ ๊ฒฝ์šฐ์— ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ํ† ๊ธ€ํ•˜๋„๋ก ์ง€์ •ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.
toggle(ํด๋ž˜์Šค๋ช…)
toggle(ํด๋ž˜์Šค๋ช…, ์กฐ๊ฑด)

 

๊ธฐ๋ณธํ˜•์€ ์ด๋ ‡๊ฒŒ 2๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.

 


 

1๋ฒˆ ๊ธฐ๋ณธํ˜•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค

// ํด๋ž˜์Šค ์ด๋ฆ„์„ dark-mode๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
toggle('dark-mode')

 

2๋ฒˆ ๊ธฐ๋ณธํ˜•๊ณผ ๊ฐ™์ด ์กฐ๊ฑด์„ ํ•จ๊ป˜ ์“ฐ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

//this๊ฐ€ ์ฒดํฌ๊ฐ€ ๋œ ๊ฒฝ์šฐ, ํด๋ž˜์Šค๋ช…์„ 'dark-mode'๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
//checked ๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์œผ๋ฏ€๋กœ this๋Š” checkbox ์ž…๋‹ˆ๋‹ค.
toggle('dark-mode', this.checked)

 

 

 

โœ… ์ „์ฒด ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>๋‹คํฌ ๋ชจ๋“œ</title>

  <!-- light-mode, dark-mode css ๊ฐ๊ฐ ๋งŒ๋“ค๊ณ 
        .light-mode     
        .wrap .dark-mode
        
        script :
          ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด toggle๋กœ 
          classList์— dark-mode ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด ~ ๋ฒ„ํŠผ text ๋ฐ”๊พธ๊ธฐ
        -->
  <style>
    *{
      box-sizing: border-box;
    }

    body{
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    button{
      position: fixed;
      top: 20px;
      right: 20px;
      padding: 10px;
      background-color: white;
      color: black;
    }

    .dark-mode{
      background-color: black;
      color: white;
    }

    .dark-mode button{
      background-color: black;
      border: 1px solid white;
      color: white;
    }

    .light-mode{
      background-color: white;
      color: black;
    }

    

  </style>

</head>

<body class="wrap light-mode">

  <div>
    <h1>Web Programming</h1>
    <ul>
      <li>HTML</li>
      <li>CSS</li>
      <li>Javascipt</li>
    </ul>
  </div>

  <button id="modeButton">
    ๋‹คํฌ ๋ชจ๋“œ
  </button>

  <script>

    document.addEventListener('DOMContentLoaded', function(){
      const modeBtn = document.querySelector('#modeButton');
      const body= document.body;

      modeBtn.addEventListener('click', (e)=>{
        body.classList.toggle('dark-mode');
        body.classList.toggle('light-mode');

        if(body.classList.contains('dark-mode')){
          modeBtn.innerText = "๋‹คํฌ ๋ชจ๋“œ";
        }else{
          modeBtn.innerText = "๋ผ์ดํŠธ ๋ชจ๋“œ";
        }

      });
    });

    //toggle ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด
    // document.addEventListener('DOMContentLoaded', function(){
    //   const modeBtn = document.querySelector('#modeButton');
    //   const body= document.body;

    //   modeBtn.addEventListener('click', (e)=>{
    //     // body.classList.toggle('dark-mode');
    //     // body.classList.toggle('light-mode');

    //     if(body.classList.contains('dark-mode')){
    //       modeBtn.innerText = "๋‹คํฌ ๋ชจ๋“œ";
    //       body.classList.remove('dark-mode');
    //       body.classList.add('light-mode');
    //     }else{
    //       modeBtn.innerText = "๋ผ์ดํŠธ ๋ชจ๋“œ";
    //       body.classList.add('dark-mode');
    //       body.classList.remove('light-mode');
    //     }

    //   });
    // });
    
  </script>
  
</body>
</html>
728x90
๋ฐ˜์‘ํ˜•