如何使用 JavaScript 切换暗/亮模式

2025-05-25

如何使用 JavaScript 切换暗/亮模式

大家好,我带着一篇关于切换暗/亮模式的精彩新文章回来了。
横幅

让我们开始吧🚀

使用的库

  • Fontawesome
  • 引导程序


  • 创造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>
Enter fullscreen mode Exit fullscreen mode
  • 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;
}
Enter fullscreen mode Exit fullscreen mode
  • 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)
Enter fullscreen mode Exit fullscreen mode

查看现场演示:https://snowbit-coderboi.github.io/dark-light-toggle/

下载代码/资产:https://github.com/snowbit-coderboi/dark-light-toggle

顺便看看我的新 YouTube 视频:https://youtu.be/R4DfuhfcHUE

希望你喜欢这篇文章;请务必在评论区分享反馈 🙂

文章来源:https://dev.to/dhairyashah/how-to-toggle-darklight-mode-using-javascript-2ecn
PREV
微软软件工程师 2 UI 面试
NEXT
React 和 JavaScript 中的未来无限滚动