当前位置:首页 > JavaScript

js实现开灯

2026-03-14 14:48:52JavaScript

使用HTML和JavaScript实现开关灯效果

通过修改网页背景色和文字颜色模拟开灯/关灯效果。以下是一个简单实现方案:

<!DOCTYPE html>
<html>
<head>
    <title>灯光控制</title>
    <style>
        body {
            transition: background-color 0.5s ease;
        }
        .dark {
            background-color: #333;
            color: white;
        }
        .light {
            background-color: white;
            color: black;
        }
    </style>
</head>
<body class="light">
    <button onclick="toggleLight()">开关灯</button>

    <script>
        function toggleLight() {
            const body = document.body;
            if (body.classList.contains('light')) {
                body.classList.remove('light');
                body.classList.add('dark');
            } else {
                body.classList.remove('dark');
                body.classList.add('light');
            }
        }
    </script>
</body>
</html>

使用CSS变量实现主题切换

更灵活的实现方式,通过CSS变量控制颜色主题:

document.addEventListener('DOMContentLoaded', () => {
    const toggleBtn = document.getElementById('theme-toggle');
    const root = document.documentElement;

    toggleBtn.addEventListener('click', () => {
        const currentTheme = root.getAttribute('data-theme');
        if (currentTheme === 'dark') {
            root.setAttribute('data-theme', 'light');
        } else {
            root.setAttribute('data-theme', 'dark');
        }
    });
});

对应的CSS部分:

:root {
    --bg-color: #ffffff;
    --text-color: #000000;
}

[data-theme="dark"] {
    --bg-color: #222222;
    --text-color: #ffffff;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    transition: all 0.3s ease;
}

使用localStorage保存用户偏好

增强用户体验,记住用户选择的主题:

function toggleTheme() {
    const root = document.documentElement;
    const currentTheme = localStorage.getItem('theme') || 'light';
    const newTheme = currentTheme === 'light' ? 'dark' : 'light';

    root.setAttribute('data-theme', newTheme);
    localStorage.setItem('theme', newTheme);
}

// 初始化时读取保存的主题
function initTheme() {
    const savedTheme = localStorage.getItem('theme') || 'light';
    document.documentElement.setAttribute('data-theme', savedTheme);
}

window.addEventListener('DOMContentLoaded', initTheme);

使用classList.toggle简化代码

更简洁的切换实现方式:

const themeBtn = document.querySelector('.theme-toggle');
themeBtn.addEventListener('click', () => {
    document.body.classList.toggle('dark-mode');
});

对应的CSS:

js实现开灯

body.dark-mode {
    background-color: #1a1a1a;
    color: #f0f0f0;
}

这些方法都可以实现基本的"开灯/关灯"效果,区别在于实现细节和扩展性。第一种方法最简单直接,后续方法提供了更好的可维护性和用户体验。

标签: 开灯js
分享给朋友:

相关文章

js实现验证码

js实现验证码

实现验证码的JavaScript方法 生成随机验证码 使用Math.random()生成随机字符串,结合数字和字母: function generateCaptcha() { const cha…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码…

js实现滑动

js实现滑动

实现滑动效果的方法 在JavaScript中实现滑动效果可以通过多种方式完成,以下是几种常见的实现方法: 使用CSS过渡和JavaScript触发 通过CSS定义过渡效果,JavaScript控制触…

js实现显示隐藏

js实现显示隐藏

显示隐藏的实现方法 在JavaScript中,实现元素的显示和隐藏可以通过多种方式完成。以下是几种常见的方法: 修改CSS的display属性 通过改变元素的display属性可以在none(隐藏)…