【问题标题】:How do I run this piece for css code through javascript如何通过 javascript 运行这段 css 代码
【发布时间】:2019-03-18 03:01:56
【问题描述】:

感谢您抽出时间对此进行研究,我们非常感谢您提供这方面的任何帮助。

我有以下代码。

.middle_n.expand.remove
{
 animation: contractm 0.3s forwards;
 transform-origin: 0 75px;
 height:200%;
 animation-delay: 1.3s;
}

@keyframes contractm 
{
 0%{}
 100%{transform:translate(147.8%,100%) rotate(-16.75deg);}
}

我希望通过javascript传递一个动态值以在contractm中旋转。我怎么做。多步动画可以通过javascript运行吗?

再次感谢。

【问题讨论】:

标签: javascript jquery css animation stylesheet


【解决方案1】:

在我看来,如果你想控制你的动画而不是让它动起来,我的建议是你可以使用 js 来控制你的元素的样式。因为animation是为了完成一系列动画。

还有一个非常强大的css叫做css变量,你可以在https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties了解更多细节。

const rotate=document.getElementById("rotate")
let r=0
rotate.addEventListener("click",()=>{
  r += 10
  rotate.style.setProperty("--rotate", r+"deg");
})
#rotate{
  --rotate:0deg;
  background:pink;
  width:100px;
  height:100px;
  transform:rotate(var(--rotate));
  transition:transform 1s;
}
<p>click the square and then it will rotate.</p>
<div id="rotate"></div>

当然,如果你想要一系列的动画,你可以在https://developer.mozilla.org/en-US/docs/Web/CSS/animation上查看。在动画中设置步骤很容易。

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 2012-10-23
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多