JavaScript를 사용하여 다크/라이트 모드를 전환하는 방법

안녕하세요, Toggle Dark/Light Mode에 대한 새로운 놀라운 기사로 돌아 왔습니다.


시작하자 🚀

사용 라이브러리


  • 폰타위썸

  • 부트스트랩

  • 만들기 index.html

  • <div class="container">
            <div class="header d-flex justify-content-between">
                <h1 class="title mt-2">Catty's Blog</h1>
                <span class="mt-4 fs-3 dark toggle"><i class="fas fa-moon d-none" id="dark" title="Switch to Dark Mode"></i></span>          
                <span class="mt-4 fs-3 light toggle" id="light"><i class="fas fa-sun" title="Switch to Light Mode"></i></span>          
            </div>
            <center>
                <div class="main">
                    <div class="">
                        <img src="assets/cat-2.webp" class="catty mt-2 rounded-circle" alt="I am Catty">
                        <div class="card-body">
                            <h1 class="card-title">
                                Hello there, I am <a href="http://github.com/snowbit-coderboi" target="_blank" class="catty-name text-decoration-none" id="cattyName">Catty</a>
                            </h1>
                            <p class="info">
                                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                            </p>
                        </div>
                    </div>
                </div>
            </center>
        </div>
    


  • style.css

  • @import url('https://fonts.googleapis.com/css2?family=Amatic+SC:wght@700&display=swap');
    @import url('https://fonts.googleapis.com/css2?family=Sacramento&display=swap');
    body{
        background-color: #212224;
        color: #fff;
        transition: 0.5s;
        -webkit-transition: 0.5s;
        -moz-transition: 0.5s;
        -ms-transition: 0.5s;
        -o-transition: 0.5s;
    }
    
    .title{
        font-family: 'Amatic SC', cursive;
        font-size: 60px;
        user-select: none;
    }
    
    .toggle{
        cursor: pointer;
        transition: 0.5s;
        -webkit-transition: 0.70s;
        -moz-transition: 0.70s;
        -ms-transition: 0.70s;
        -o-transition: 0.70s;
    }
    #light:hover{
        color: yellow;
    }
    
    .catty-name{
        color: #fff;
        transition: 0.3s;
        -webkit-transition: 0.3s;
        -moz-transition: 0.3s;
        -ms-transition: 0.3s;
        -o-transition: 0.3s;
    }
    .catty-name:hover{
        color: #fff;
        transition: 0.7s;
        -webkit-transition: 0.7s;
        -moz-transition: 0.7s;
        -ms-transition: 0.7s;
        -o-transition: 0.7s;
        font-size: 70px;
    }
    .main{
        margin-bottom: 15px;
        margin-top: 20px;
    }
    .card-title{
        font-family: 'Sacramento', cursive;
        font-size: 60px;
    }
    .card-list{
        /* font-family: 'Sacramento', cursive; */
        font-size: 30px;
        list-style: none;
    }
    .card-list li:before { content: '😸'; margin-left: -10px; margin-right: 10px; } 
    
    .catty{
        max-height: 200px;
        object-fit: contain;
        /* transform: rotate(20deg); */
        transition: 0.70s;
        -webkit-transition: 0.70s;
        -moz-transition: 0.70s;
        -ms-transition: 0.70s;
        -o-transition: 0.70s;
    }
    .catty:hover{
        transition: 0.70s;
      -webkit-transition: 0.70s;
      -moz-transition: 0.70s;
      -ms-transition: 0.70s;
      -o-transition: 0.70s;
      -webkit-transform: rotate(350deg);
      -moz-transform: rotate(350deg);
      -o-transform: rotate(350deg);
      -ms-transform: rotate(350deg);
      transform: rotate(360deg);
    }
    .catty{
        max-height: 200px;
        object-fit: contain;
        /* transform: rotate(20deg); */
        transition: 0.70s;
        -webkit-transition: 0.70s;
        -moz-transition: 0.70s;
        -ms-transition: 0.70s;
        -o-transition: 0.70s;
    }
    .info{
        text-align: justify;
        line-height: 2em;
        max-width: 900px;
        font-size: 18px;
    }
    


  • index.js

  • const dark = document.getElementById('dark')
    const light = document.getElementById('light')
    const catty = document.getElementById('cattyName')
    
    function toggleDark(){
        dark.classList.add('d-none')
        light.classList.remove('d-none')
        document.body.style.backgroundColor = "#212224"
        document.body.style.color = "#fff"
        catty.style.color = "#fff"
    }
    
    dark.addEventListener('click', toggleDark)
    
    function toggleLight(){
        light.classList.add('d-none')
        dark.classList.remove('d-none')
        document.body.style.backgroundColor = "#fff"
        document.body.style.color = "#000"
        catty.style.color = "#000"
    }
    light.addEventListener('click', toggleLight)
    


    라이브 데모를 확인하십시오: https://snowbit-coderboi.github.io/dark-light-toggle/

    코드/자산 다운로드: https://github.com/snowbit-coderboi/dark-light-toggle

    그나저나 제 새 YouTube 동영상을 확인하세요: https://youtu.be/R4DfuhfcHUE

    이 게시물이 마음에 드셨기를 바랍니다. 댓글 섹션에서 피드백을 공유하세요 🙂

    좋은 웹페이지 즐겨찾기