简体   繁体   English

如何增加 CSS animation 的长度?

[英]How can I increase the length of a CSS animation?

This code is supposed to style button when clicked and to increase the length of the style even after click for better looking.此代码应该在单击时设置按钮样式,并在单击后增加样式的长度以获得更好的外观。 Or is there any other way to do it?或者有没有其他方法可以做到这一点? Thanks.谢谢。

// When button is clicked, apply longer style so it looks better
document.querySelectorAll('button[type="button"]').forEach(button => {
  button.addEventListener("click",event => {
    //Apply style
    event.target.style["boxShadow"] = "0 0 2px 3px rgba(250,189,22,0.95), inset 0 0 4px 0 rgba(250,195,35,1)";
    event.target.style.borderRadius = "0.4rem";
    event.target.style.transitionDuration = "45ms";
    event.target.style.transform = "scale(0.98)";
    setTimeout(() => {
    //Remove the clicked style. Same as before   
    event.target.style.removeProperty("box-shadow");
    event.target.style.removeProperty('border-radius');
    event.target.style.removeProperty('transition-duration');
    event.target.style.removeProperty('transform');
    }, 110);
  });
});

Your way of doing should work perfectly fine.你的做事方式应该工作得很好。 Even though i would move the click specific styling to a CSS class and add the classname to the button when clicked and remove after given amount of ms.即使我会将单击特定样式移动到 CSS class 并在单击时将类名添加到按钮并在给定的毫秒数后删除。

document.querySelectorAll('button[type="button"]').forEach(button => {
  button.addEventListener("click",event => {
    event.target.classList.add("beautiful")
    setTimeout(() => {
      event.target.classList.remove("beautiful")
    }, 110);
  });
});

In your CSS you then would have the class "beautiful".在您的 CSS 中,您将拥有“美丽”的 class。

button {
  transition-duration: 45ms; // needs to be moved to regular button style
}

.beautiful {
  box-shadow: 0 0 2px 3px rgba(250,189,22,0.95), inset 0 0 4px 0 rgba(250,195,35,1);
  border-radius: 0.4rem;
  transform: scale(.98);
}

Add this class to all button将此 class 添加到所有按钮

buttonClass{
  // regular button css 
  transition: transform 0.5s;  // set time for effect
}

buttonClass:active {
  // CSS for on button click
  transition:  0s;
}

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

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