语境:
If a ‘0%’ or ‘from’ keyframe is not specified,then the user agent
constructs a ‘0%’ keyframe using the computed values of the properties
being animated. If a ‘100%’ or ‘to’ keyframe is not specified,then
the user agent constructs a ‘100%’ keyframe using the computed values
of the properties being animated.
这可能导致两种不同的解释:
> A)在类中声明属性,而不是在0%或关键帧中声明属性.
> B)声明类中的属性以及0%或关键帧中的属性.
简化示例:
p:first-of-type { opacity: 0; animation: a 3s linear infinite alternate; } @keyframes a { 100% { opacity: 1; } } p:last-of-type { opacity: 0; animation: b 3s linear infinite alternate; } @keyframes b { 0% { opacity: 0; } 100% { opacity: 1; } }
<p> A) Declare the property in the class <strong>and not</strong> in the `0%` or `from` keyframe. </p> <p> B) Declare the property in the class <strong>and </strong> in the `0%` or `from` keyframe. </p>
虽然两者具有相同的最终结果,但遵循Don’t Repeat Yourself (DRY)原则,A)可以大大减少使用多个属性的所有动画的代码.
复杂的例子:
/* Layout */ * { Box-sizing: border-Box; } body { margin: 0; } ul { margin: 0; padding: 0; list-style: none; } .container { height: 100vh; display: flex; flex-direction: column; justify-content: space-around; counter-reset: list-item; } .container > li { position: relative; counter-increment: list-item; } .container > li::before { content: counter(list-item,upper-alpha); position: absolute; top: 0; left: 0; right: 0; bottom: 0; width: 100%; height: 100%; display: flex; justify-content: flex-end; align-items: flex-end; font-size: 1.5em; background-color: moccasin; } .container > li::after { content: ''; position: absolute; top: 50%; left: 0; width: 100%; transform: translateY(-50%); height: 2px; background-color: gold; } /* SVG */ .svg-spritesheet { display: none; } .svg__icon { display: inline-block; vertical-align: middle; width: 1em; height: 1em; } .svg__icon--square { font-size: 5em; color: dodgerblue; } /* Question Related */ .container > li:first-of-type .svg__icon--square { opacity: 0; transform: scale(.5) rotate(45deg); animation: animationA 5s linear infinite alternate; } .container > li:nth-child(2) .svg__icon--square { opacity: 0; transform: scale(.5) rotate(45deg); animation: animationB 5s linear infinite alternate; } @keyframes animationA { to { opacity: 1; transform: translateX(500px) scale(1) rotate(90deg); } } @keyframes animationB { from { opacity: 0; transform: scale(.5) rotate(45deg); } to { opacity: 1; transform: translateX(500px) scale(1) rotate(90deg); } }
<ul class="container"> <li> <svg class="svg__icon svg__icon--square"> <use xlink:href="#svg-icon-square"></use> </svg> </li> <li> <svg class="svg__icon svg__icon--square"> <use xlink:href="#svg-icon-square"></use> </svg> </li> </ul> <svg class="svg-spritesheet"> <symbol id="svg-icon-square" viewBox="0 0 32 32"> <title>Demo Square</title> <rect width="32" height="32" fill="currentColor" /> </symbol> </svg>
题:
如果已经在应用动画的类中声明了CSS动画的0%关键帧中的属性,是否安全?
这样做:
.el { opacity: 0; animation: example 1s; } @keyframes example { 100% { opacity: 1; } }
跨浏览器认为安全?或者是否期望用户代理呈现不同的结果或性能?
测试:
渲染相同的结果:
Windows 10/64位
> Chrome 54.0.2840.71米(64位)
> FireFox 49.0.2
> Edge 25.10586.0.0