简体   繁体   English

返回到原始 css 变量值的 JavaScript 切换按钮问题

[英]JavaScript toggle button problem with reverting back to original css variable values

Im trying to make a toggle switch that changes my color variables I have set.我试图制作一个切换开关来改变我设置的颜色变量。 The following code works but I want it to revert back to the original colors if clicked again.以下代码有效,但如果再次单击,我希望它恢复到原始颜色。 Also, if there is a better way to accomplish that Id like to learn how, this seemed to be the most direct way but I am still learning vanilla JS.另外,如果有更好的方法来完成我想学习的方法,这似乎是最直接的方法,但我仍在学习 vanilla JS。

css: css:

:root {
  --bg-color:  #040d14;
  --text-color:  #f0f4f6;
  --border-color: #30363a;
  --highlight-color: #00c805; 
  --secondary-bg-color:#1e2124;
}

js: js:

change.onclick = () => {
  document.documentElement.style.setProperty('--bg-color', '#f7f7f7');
  document.documentElement.style.setProperty('--text-color', '#333');
  document.documentElement.style.setProperty('--border-color', '#040d14');
  document.documentElement.style.setProperty('--secondary-bg-color', '#ebecec');
 
}

So to me this looks like you are toggling between a dark/light mode.所以对我来说,这看起来像是在暗/亮模式之间切换。 There are a ton of tutorials out there on this but this is the basics of it.有很多关于此的教程,但这是它的基础知识。

 const button = document.querySelector('button') button.addEventListener('click', toggleTheme) function toggleTheme() { if (document.body.getAttribute('data-theme') === 'light') { document.body.setAttribute('data-theme', 'dark') } else { document.body.setAttribute('data-theme', 'light') } }
 :root { --bg-color: #040d14; --text-color: #f0f4f6; --border-color: #30363a; --highlight-color: #00c805; --secondary-bg-color:#1e2124; } [data-theme="light"] { --bg-color: #f7f7f7; --text-color: #333; --border-color: #040d14; --highlight-color: #ebecec; --secondary-bg-color:#1e2124; } .container { background-color: var(--bg-color); height: 200px; color: var(--text-color); border: 1px solid var(--border-color); }
 <div class="container"> Stuff </div> <button>Toggle</button>

I think your way is the best way.我认为你的方式是最好的方式。 To remove, you can use .removeProperty() .要删除,您可以使用.removeProperty()

document.documentElement.style.removeProperty("--bg-color")

Edit编辑

Example of how to toggle between two states.如何在两种状态之间切换的示例。 You could also set the initial CSS custom property using JavaScript, which can be useful if you have multiple options that are not predefined.您还可以使用 JavaScript 设置初始 CSS 自定义属性,如果您有多个未预定义的选项,这会很有用。

<div class="container"></div>
:root {
    --bg: red;
}
.container {
    width: 200px;
    height: 200px;
    background: var(--bg);
}
let theme = "red"
const container = document.querySelector(".container")
container.addEventListener("click", function() {
    if (theme === "red") {
        theme = "blue"
        document.documentElement.style.setProperty("--bg", "blue")
        return
    }

    theme = "red"
    document.documentElement.style.removeProperty("--bg")
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM