简体   繁体   English

如何在js中实现class绑定

[英]how to implement a class bindings in js

I just started front-end, want to implement a dark mode animation.刚开始做前端,想实现一个深色模式animation。

When you click the switch(a rope), the page will change theme.当你点击开关(一根绳子)时,页面将改变主题。

but I don't know how to add the animation by using js like vue's class binding.但我不知道如何使用 vue 的 class 绑定之类的 js 添加 animation。


@keyframes line {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(100px);
  }
  100% {
    transform: translateY(0);
  }
}

.pull {
  animation: line 0.5s;
}

just like this in vue(I found it online)在vue中就像这样(网上找的)

<div
  class="switch"
  :class="{ 'pull': inAnimation }"
  @animationend="animationEnd"
  @click="changeTheme"
/>

This is my code.这是我的代码。

I don't konw how to use js to complete it.我不知道如何使用js来完成它。

const sw = document.getElementById('switch');

const changeTheme = () => {
  sw.addEventListener("click", () => {
    sw.className = 'pull';
    if (document.body.className !== "dark") {
      document.body.classList.add("dark");
      localStorage.setItem("css", "dark");
    } else {
      document.body.classList.remove("dark");
      localStorage.removeItem("css");
    }
    sw.className = '';
  });

  if (localStorage.getItem("css")) {
    document.body.className = "dark";
    document.body.classList.add("dark");
  }
  
};

changeTheme();

First, I see that you miss the id the element because you're using getElementById , it has to be like:首先,我看到您错过了元素的 id,因为您使用的是getElementById ,它必须是这样的:

<div
  id="switch" <!-- this is missing -->
  class="switch"
  :class="{ 'pull': inAnimation }"
  @animationend="animationEnd"
  @click="changeTheme"
/>

Then, Based on using Vue or not.然后,基于是否使用Vue。 (If Vue, setup the code in same component as in the element above). (如果是 Vue,请在与上述元素相同的组件中设置代码)。

Vue视图

<script>

export default {
    mounted() {
        const sw = document.getElementById('switch');
        sw.addEventListener("click", () => {
            sw.classList.add('pull');
            if (!document.body.classList.contains("dark")) {
            document.body.classList.add("dark");
            localStorage.setItem("css", "dark");
            } else {
            document.body.classList.remove("dark");
            localStorage.removeItem("css");
            }
            setTimeout(() => {
              sw.classList.remove("pull")
            }, 600);
        });

        if (localStorage.getItem("css")) {
            document.body.classList.add("dark");
        }   
    }
}
</script>

if Vanilla Js如果香草 Js

const sw = document.getElementById("switch");
if (sw) {
  const changeTheme = () => {
    sw.addEventListener("click", () => {
      sw.classList.add("pull");
      if (!document.body.classList.contains("dark")) {
        document.body.classList.add("dark");
        localStorage.setItem("css", "dark");
      } else {
        document.body.classList.remove("dark");
        localStorage.removeItem("css");
      }
      setTimeout(() => {
        sw.classList.remove("pull");
      }, 600);
    });

    if (localStorage.getItem("css")) {
      document.body.classList.add("dark");
    }
  };
  changeTheme();
}

I'm also a starter, not sure if my method can satisfy you .我也是初学者,不知道我的方法能不能满足你。 I prefer to use v-bind to bind the class of the element.我更喜欢使用 v-bind 来绑定元素的类。 But if you insist on js, you can use For example: You have a navigation bar:但是如果你坚持使用js,你可以使用例如:你有一个导航栏:

<ul id="nav" class="nav1">
  <li>.....</li>
  <button click="changeTheme"> change theme</button>
</ul>

you can first write down two themes:你可以先写下两个主题:

<style>
    .nav1 {
      margin-top: 10%;
      width: 100px;
      height: 100%;
      margin: 0px;
      padding: 0px;
      background-color: black;
      position: fixed;
      transition :700ms;
    }
    .nav2{
      margin-top: 10%;
      width: 100px;
      height: 100%;
      margin: 0px;
      padding: 0px;
      background-color: white;
      position: fixed;
      transition:700ms;
    }
</style>

these css is the style of two themes, and when animation isn't that complicated (like just switch between two themes), you can use transition.这些css是两个主题的样式,当动画不是那么复杂(比如在两个主题之间切换)时,可以使用transition。 And use js to response the button' event and change the class by id:并使用 js 来响应按钮的事件并通过 id 更改类:

<script>    
  function changeTheme(){
    if(document.getElementById("nav").className=="nav1"){
      document.getElementById("nav").setAttribute('class','nav2');
    }
    else {document.getElementById("nav").setAttribute('class','nav1');
    }
  }
</script>

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

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