HTML5一键切换白天黑夜特效

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>一键切换白天黑夜特效</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            transition: background-color 0.5s ease;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }

        body.day {
            background-color: #f0f9ff;
            color: #333;
        }

        body.night {
            background-color: #1a202c;
            color: #fff;
        }

        #toggle-btn {
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            background-color: #4299e1;
            color: white;
            transition: background-color 0.3s ease;
        }

        #toggle-btn:hover {
            background-color: #3182ce;
        }

        #link {
            position: absolute;
            bottom: 20px;
            right: 20px;
        }

        #link a {
            color: inherit;
            text-decoration: none;
            transition: color 0.3s ease;
        }

        #link a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body class="day">
    <h1>欢迎体验白天黑夜切换</h1>
    <button id="toggle-btn">切换模式</button>
    <div id="link">

    </div>
    <script>
        const toggleBtn = document.getElementById('toggle-btn');
        const body = document.body;

        toggleBtn.addEventListener('click', function () {
            if (body.classList.contains('day')) {
                body.classList.remove('day');
                body.classList.add('night');
                toggleBtn.textContent = '切换到白天';
            } else {
                body.classList.remove('night');
                body.classList.add('day');
                toggleBtn.textContent = '切换到黑夜';
            }
        });
    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值