简体   繁体   English

Javascript获取元素CSS过渡阶段

[英]Javascript Get Element CSS Transition Stage

Is it possible to get which stage of a CSS transition an object is in with Javascript, without the use of a interval, or relying on a starting timestamp? 是否可以使用Javascript获取CSS转换的哪个阶段,而不使用间隔或依赖于开始时间戳? I know that transitions can be strung together, but I'd rather avoid that if I can. 我知道过渡可以串联在一起,但如果可以,我宁愿避免过渡。

For example for a div going through the following transition: 例如,对于经历以下过渡的div:

@keyframes example {
  0% {background: red;}
  50% {background: blue;}
  100% {background: yellow;}
}

Is it possible to find if the div is between 0% and 50%? 是否可以找到div是否介于0%和50%之间? Pure Javascript please. 纯Javascript请。

You can assign a property in the animation and retrieve this property value to know the animation stage. 您可以在动画中指定属性并检索此属性值以了解动画阶段。 For instance, asign z-index a value between 100 and 200: 例如,将z-index设置为100到200之间的值:

click the element to show the percentage of the animation 单击元素以显示动画的百分比

 function showStep () { var ele = document.getElementById("test"); var step = getComputedStyle(ele).getPropertyValue("z-index") - 100; ele.innerText = step; } 
 #test { position: absolute; width: 400px; height: 200px; border: solid red 1px; -webkit-animation: colors 4s infinite; animation: colors 6s infinite; font-size: 80px; color: white; } @-webkit-keyframes colors { 0% {background: red; z-index: 100;} 50% {background: blue; z-index: 150;} 100% {background: yellow; z-index: 200;} } @keyframes colors { 0% {background: red; z-index: 100;} 50% {background: blue; z-index: 150;} 100% {background: yellow; z-index: 200;} } 
 <div id="test" onclick="showStep();"> </div> 

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

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