首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >加载期间暗模式不工作

加载期间暗模式不工作
EN

Stack Overflow用户
提问于 2020-10-02 15:16:36
回答 5查看 674关注 0票数 3

我正在尝试制作一个深色模式切换按钮,它可以在点击时在深色和浅色模式之间切换,用户偏好也可以使用localStorage存储。用户应手动按下该按钮以切换到其他模式。如果用户选择的是深色模式,则每个页面都将处于深色模式,刷新时不会转到浅色模式。到目前为止,一切看起来都很好,但真正的问题来自于加载时间。页面的加载时间接近1秒,在这段时间内,即使用户选择的是暗模式,页面也会显示为亮模式。我不希望这样的事情发生。我希望加载时间段在黑暗模式,如果用户的选择是黑暗的。这是我当前的代码:

代码语言:javascript
运行
复制
<script>
const body = document.querySelector('body');
function toggleDark() {
  if (body.classList.contains('dark')) {
    body.classList.remove('dark');
    localStorage.setItem("theme", "light");
  } else {
    body.classList.add('dark');
    localStorage.setItem("theme", "dark");
  }
}

if (localStorage.getItem("theme") === "dark") {
  body.classList.add('dark');
}
</script>
<style>
body {background-color: #ffffff}
body.dark {background-color: #000000; color: #ffffff} 
</style>
<button class="dark-mode" id="btn-id" onclick="toggleDark()"></button>
EN

回答 5

Stack Overflow用户

发布于 2020-10-02 16:13:48

另一种选择是在<head>元素中加载脚本,并在html元素上切换类

为此,您可以使用document.documentElement.classList,因为它是HTML元素

然后将您的CSS更改为

代码语言:javascript
运行
复制
html.dark body {}

等等。HTML上的类选择器

代码语言:javascript
运行
复制
html body {background-color: #ffffff}
html.dark body {background-color: #000000; color: #ffffff}
代码语言:javascript
运行
复制
<script>
  const body = document.querySelector('body');

  function toggleDark() {
    if (document.documentElement.classList.contains('dark')) {
      document.documentElement.classList.remove('dark');
      //localStorage.setItem("theme", "light");
    } else {
      document.documentElement.classList.add('dark');
      //localStorage.setItem("theme", "dark");
    }
  }

  //if (localStorage.getItem("theme") === "dark") {
    document.documentElement.classList.add('dark');
  //}
</script>
<button class="dark-mode" id="btn-id" onclick="toggleDark()">DARK</button>

由于限制,堆栈溢出时localStorage不可用-请取消注释这些行以查看其工作情况

或者-请参阅https://jsfiddle.net/e9zg2p4c/

票数 1
EN

Stack Overflow用户

发布于 2020-10-02 15:23:12

存储到后端数据库。然后,在提供HTML内容时,为您的元素放置适当的类/样式。这将消除加载时间之间的闪烁:

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
    <head>
        <style>
            /* Use Less/Sass for better management */
            .theme.light {
                background-color: #ffffff;
            }
            .theme.dark {
                background-color: #000000; color: #ffffff;
            }
        </style>
    </head>
    <body class="theme <?= $user->themeName; ?>">

    </body>
</html>
票数 0
EN

Stack Overflow用户

发布于 2020-10-02 15:42:31

切换起来有点棘手,并且有一个默认的主题

注意,localStorage调用在这里是不起作用的

working example

在下面的代码中替换

使用localStorage.getItem("theme") || "light"const theme = "dark";

并取消对// localStorage.setItem("theme", body.classList.contains("dark") ? "light" : "dark");的注释

在服务器上

代码语言:javascript
运行
复制
.dark { background-color: black; color: white; }
代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>

<head>
  <style>
    .theme {
      background-color: white;
      color: black;
    }
  </style>
  <script>
    const theme = "dark"; // localStorage.getItem("theme") || "theme"
    if (theme === "dark") {
      const st = document.createElement("style");
      st.id="darkStyle";
      st.innerText = `body.theme { background-color: black; color: white; }`;
      document.querySelector("head").appendChild(st);
    }

    window.addEventListener("load", function() {
      document.getElementById("toggleTheme").addEventListener("click", function() {
        const body = document.querySelector("body");      
        const darkStyle = document.getElementById("darkStyle");
        if (darkStyle) {
          darkStyle.remove(); // remove stylesheet now we know what the user wants
          body.classList.remove("theme");
        }  
        const theme = body.classList.contains("theme"); 
        body.classList.toggle('theme',!theme);
        body.classList.toggle('dark',theme);
        // localStorage.setItem("theme", theme ? "light" : "dark"); // uncomment on your server
      });
    })
  </script>



</head>

<body class="theme">
  Here is the body

  <button id="toggleTheme" type="button">Toggle theme</button>
</body>

</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64167519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档