简体   繁体   English

从中间状态的JavaScript Web Animations API读取样式信息

[英]Read style information from JavaScript Web Animations API, mid state

Given a web animation, such as the one over here: https://codepen.io/arxpoetica/pen/aXqEwe 给定一个网络动画,例如此处的动画: https : //codepen.io/arxpoetica/pen/aXqEwe

How do I get CSS or style information based on state between keyframes at any given point and time? 如何在任何给定的时间点基于关键帧之间的状态获取CSS或样式信息?

Sample code: 样例代码:

<div class="thing"></div>

<style>
html {
  height: 100%;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #0a2933;
}
.thing {
  height: 100px;
  width: 100px;
  background-color: orange;
}
</style>

<script>
document.querySelector('.thing').animate([
    { transform: 'rotate(0) scale(1)', backgroundColor: 'orange' },
    { transform: 'rotate(180deg) scale(3)', backgroundColor: 'blue' },
    { transform: 'rotate(360deg) scale(1)', backgroundColor: 'orange' }
], {
    duration: 3000,
    iterations: Infinity
})
</script>

NOTE: this API only works in limited browsers / platforms. 注意:此API仅在有限的浏览器/平台上有效。

WAAPI method .animate returns Animation object which includes all you need: WAAPI方法.animate返回Animation对象,其中包括您需要的所有内容:

Run the snippet below: 运行以下代码段:

 var keyframes = [{ transform: 'rotate(0) scale(1)', backgroundColor: 'orange' }, { transform: 'rotate(180deg) scale(3)', backgroundColor: 'blue' }, { transform: 'rotate(360deg) scale(1)', backgroundColor: 'orange' } ]; var options = { duration: 3000, iterations: Infinity }; var thing = document.querySelector('.thing'); var A = thing.animate(keyframes, options); console.clear(); function getState() { if (A.playState === 'running') { document.querySelector('section').innerHTML += `<p> progress: ${getProgress()*100|0}% <br> transform: ${getComputedStyle(thing).transform} <br> backgroundColor: ${getComputedStyle(thing).backgroundColor} </p>`; //A.startTime = 0; A.pause(); } else { A.play(); document.querySelector('section p:last-of-type').remove(); } } function getProgress() { return A.effect ? A.effect.getComputedTiming().progress : A.currentTime % options.duration / options.duration } 
 html { height: 100%; } body { display: flex; justify-content: center; align-items: center; height: 100%; background-color: #0a2933; } section { position: absolute; z-index: 1; top: 0; left: 1em; color: silver } .thing { height: 100px; width: 100px; background-color: orange; } 
 <!-- polyfill --> <script src="https://rawgit.com/web-animations/web-animations-js/master/web-animations.min.js"></script> <section> <p>How do I get CSS or style information based on state between keyframes at any given point and time?</p> <button onclick="getState()">Click here</button> </section> <div class="thing"></div> 

Also, WAAPI has a nice polyfill . 此外,WAAPI有一个很好的填充工具

Hope it helps. 希望能帮助到你。

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

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