类列表 onclick 的多重切换

发布于 2025-01-20 17:32:57 字数 832 浏览 3 评论 0原文

大家好,这是我在这个网站上的第一篇文章,我想知道是否可以有多个 classList,并且每次单击元素时都会切换,并在最后一个 classList 之后循环,

这是我的 css:

:root {
  --primCol: #fff;
}

.dark-theme{
  --primCol: #9A4747;
}

body {
  display: inline-flex;
  width: 100vw;
  height: 100vh;
  background-color: var(--primCol);
  margin: 0;
  padding: 0;
  justify-content: center;
  align-items: center;
  transition: all .5s;
}

#wrapper {
  transition: all .7s;
  border-radius: 5px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  background-color: var(--primCol);
  width: 300px;
  height: 300px;
  box-shadow: 0 0 10px #B8B8B8;
}

#wrapper:active {
  transform: scale(1.3);
}

我的 jQuery:

$(document).ready(() => {
  $('#wrapper').click( () => {
    document.body.classList.toggle("dark-theme");
  });
});

good day everyone this is my first post on this site, i am wondering if it's possible to have multiple classList, and will be toggle each click on the element and loop after the last classList

here's my css:

:root {
  --primCol: #fff;
}

.dark-theme{
  --primCol: #9A4747;
}

body {
  display: inline-flex;
  width: 100vw;
  height: 100vh;
  background-color: var(--primCol);
  margin: 0;
  padding: 0;
  justify-content: center;
  align-items: center;
  transition: all .5s;
}

#wrapper {
  transition: all .7s;
  border-radius: 5px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  background-color: var(--primCol);
  width: 300px;
  height: 300px;
  box-shadow: 0 0 10px #B8B8B8;
}

#wrapper:active {
  transform: scale(1.3);
}

my jQuery:

$(document).ready(() => {
  $('#wrapper').click( () => {
    document.body.classList.toggle("dark-theme");
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2025-01-27 17:32:57

试试这个。然后,使用 ::root 中的变量为元素提供样式。

$('#theme-switcher').click(function() {
  document.body.classList.toggle("dark-theme");
});
:root {
  --primCol: #fff;
}

::root.dark-theme {
  --primCol: #9A4747;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="theme-switcher">Toggle Theme</button>

Try this. Then, use variables from your ::root to give styles to elements.

$('#theme-switcher').click(function() {
  document.body.classList.toggle("dark-theme");
});
:root {
  --primCol: #fff;
}

::root.dark-theme {
  --primCol: #9A4747;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="theme-switcher">Toggle Theme</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文