【问题标题】:Animate text element by adding a class and animate it back to initial color by removing the class通过添加类为文本元素设置动画,并通过删除类将其设置回初始颜色
【发布时间】:2020-06-13 20:46:32
【问题描述】:

如果出于某种原因我不想为我的元素指定颜色类(我只是给它一个类名以便能够选择它,但我希望颜色类不附加到元素上)

如何在颜色之间切换?我的意思是将颜色从蓝色变为红色,然后再从红色变为蓝色......

这是我在这里的一个丑陋的解决方案,但我认为应该有一个更好和更清洁的方法,可能使用纯 CSS,比如切换一个类,当附加类时,如果删除元素动画回红色,则元素是蓝色的。 ..

const text = document.querySelector(".myClass");

text.addEventListener("click", function (){
  
  if(text.classList.contains("changeBlue")){
     text.classList.add("changeRed");
     text.classList.remove("changeBlue");
  } else if(text.classList.contains("changeRed")){
     text.classList.add("changeBlue");
     text.classList.remove("changeRed");
  } else {
     text.classList.add("changeRed");
  }
  
  
})
@keyframes changeRedAnime {
  from {color: blue}
  to   {color: red}
}

.changeRed {
  animation-name: changeRedAnime;
  animation-duration: .5s;
  animation-fill-mode: forwards;
}

@keyframes changeBlueAnime {
  from {color: red}
  to   {color: blue}
}

.changeBlue {
  animation-name: changeBlueAnime;
  animation-duration: .5s;
  animation-fill-mode: forwards;
}

.myClass {
  user-select:none;
  color:blue;
  cursor:pointer;
}
<div class="myClass">This is a sample text.</div>

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    另一种方法是将 css 类名存储在数组中并使用一些 index-access-increment-modulo 魔法

    示例

    const text = document.querySelector(".myClass");
    const classList = text.classList;
    const values = ["changeBlue", "changeRed"]
    let it = 0;
    
    text.addEventListener("click", function () {
      classList.remove(values[it++ % 2]);
      classList.add(values[it % 2]);
    })
    @keyframes changeRedAnime {
      from {color: blue}
      to   {color: red}
    }
    
    .changeRed {
      animation-name: changeRedAnime;
      animation-duration: .5s;
      animation-fill-mode: forwards;
    }
    
    @keyframes changeBlueAnime {
      from {color: red}
      to   {color: blue}
    }
    
    .changeBlue {
      animation-name: changeBlueAnime;
      animation-duration: .5s;
      animation-fill-mode: forwards;
    }
    
    .myClass {
      user-select:none;
      color:blue;
      cursor:pointer;
    }
    <div class="myClass">This is a sample text.</div>

    【讨论】:

      【解决方案2】:

      @Józef Podlecki 的回答中有很好的 JS,但我也想改进你的 CSS。 animation 不需要如此庞大的结构。 transition: color .5s linear; 就足够了。

      或者您可以从一开始就设置一些默认颜色类,然后切换类。

      const text = document.querySelector(".myClass");
      text.addEventListener("click", function (){
        text.classList.toggle("red");
        text.classList.toggle("blue");    
      })
      .myClass {
        user-select:none;
        cursor:pointer;
        transition: color .5s linear;
      }
      .red {
        color: red; 
      }
      .blue {
        color: blue; 
      }
      <div class="myClass blue">This is a sample text.</div>

      【讨论】:

      • 更新了一点。请看。
      • “.myClass:not(red) {color:blue;}”是否也足够了,所以只需要切换一个类吗?
      • 我认为它也会起作用。但是你有一个小错误。 “.myClass:not(.red) {color:blue;}”。您忘记了“红色”之前的点
      • ups,是的,点不见了,谢谢你发现这个......遗憾的是没有评论编辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      相关资源
      最近更新 更多