使用html实现主题的亮暗切换

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>body添加样式实现主题切换</title>
  <style>
    body {
      height: 100vh;
      padding: 0;
      margin: 0;
      background: #fff;
    }
    body.night {
      background: #031f30;
    }

    .card {
      position: absolute;
      top: 100px;
      left: 100px;
      width: 300px;
      padding: 24px;
      /*  */
      background: #fff;
      box-shadow: 0 3px 6px rgb(11 33 74 / 9%), 0 -2px 2px rgb(11 33 74 / 3%);
    }
    body.night .card {
      background: #0e161b;
    }

    .title {
      color: #33444d;
    }
    body.night .title {
      color: #fff;
    }

    .content {
      color: #999;
    }
    body.night .content {
      color: #9cd4f7;
    }
  </style>
</head>

<body>
  <div class="card">
    <h2 class="title">卡片标题</h2>
    <p class="content">卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容</p>
  </div>
  <button onclick="changeTheme()">亮暗切换</button>
  <script>
    function changeTheme() {
      if (document.body.classList.contains("night")) {
        document.body.classList.remove("night");
      } else {
        document.body.classList.add("night");
      }
    }
  </script>
</body>

</html>
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>css变量实现主题切换</title>
  <style>
    :root {
      --main-bg: #fff;
      --content-bg: #fff;
      --primary-color: #33444d;
      --secondary-color: #999;
    }
    .night-theme {
      --main-bg: #031f30;
      --content-bg: #0e161b;
      --primary-color: #fff;
      --secondary-color: #9cd4f7;
    }
    body {
      height: 100vh;
      padding: 0;
      margin: 0;
      background: var(--main-bg);
    }
    .card {
      position: absolute;
      top: 100px;
      left: 100px;
      width: 300px;
      padding: 24px;
      /*  */
      background: var(--content-bg);
      box-shadow: 0 3px 6px rgb(11 33 74 / 9%), 0 -2px 2px rgb(11 33 74 / 3%);
    }
    .title {
      color: var(--primary-color);
    }
    .content {
      color: var(--secondary-color);
    }
  </style>
</head>
<body>
  <div class="card">
    <h2 class="title">卡片标题</h2>
    <p class="content">卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容卡片内容</p>
  </div>
  <button onclick="changeTheme()">亮暗切换</button>
  <script>
    // function changeTheme() {
    //   if (document.body.classList.contains("night-theme")) {
    //     document.body.classList.remove("night-theme");
    //   } else {
    //     document.body.classList.add("night-theme");
    //   }
    // }
    // 变更变量
    // var root = document.querySelector(':root');
    // var root = '--main-bg: #fff;--content-bg: #fff;--primary-color: #33444d;--secondary-color: #999;'
    // var night = '--main-bg: #031f30;--content-bg: #0e161b;--primary-color: #fff;--secondary-color: #9cd4f7;'
    // var isNight = false;
    // function changeTheme() {
    //   root.setAttribute('style', isNight ? root : night );
    //   isNight = !isNight;
    // }
    // 第二种
    var root = document.documentElement
    var isNight = false;
    function changeTheme() {
      if (isNight) {
        root.style.setProperty('--main-bg', '#031f30');
        root.style.setProperty('--content-bg', '#0e161b');
        root.style.setProperty('--primary-color', '#fff');
        root.style.setProperty('--secondary-color', '#9cd4f7');
      } else {
        root.style.setProperty('--main-bg', '#fff');
        root.style.setProperty('--content-bg', '#fff');
        root.style.setProperty('--primary-color', '#33444d');
        root.style.setProperty('--secondary-color', '#999');
      }
      isNight = !isNight;
    }
  </script>
</body>
</html>
相关推荐
N***73851 小时前
Vue网络编程详解
前端·javascript·vue.js
e***71671 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
程序猿小蒜1 小时前
基于springboot的的学生干部管理系统开发与设计
java·前端·spring boot·后端·spring
银空飞羽1 小时前
让Trae CN SOLO自主发挥,看看能做出一个什么样的项目
前端·人工智能·trae
Eshine、2 小时前
解决前端项目中,浏览器无法正常加载带.gz名称的文件
前端·vue3·.gz·.gz名称的js文件无法被加载
用户47949283569152 小时前
别再当 AI 的"人肉定位器"了:一个工具让 React 组件秒定位
前端·aigc·ai编程
WYiQIU3 小时前
面了一次字节前端岗,我才知道何为“造火箭”的极致!
前端·javascript·vue.js·react.js·面试
qq_316837753 小时前
uniapp 观察列表每个元素的曝光时间
前端·javascript·uni-app
小夏同学呀3 小时前
在 Vue 2 中实现 “点击下载条码 → 打开新窗口预览 → 自动唤起浏览器打印” 的功能
前端·javascript·vue.js
芳草萋萋鹦鹉洲哦3 小时前
【vue】导航栏变动后刷新router的几种方法
前端·javascript·vue.js