gpt4 book ai didi

css - 如果已经在应用动画的类中声明了某个属性,那么从 CSS 动画的 `0%` 关键帧中删除该属性是否安全?

转载 作者:技术小花猫 更新时间:2023-10-29 11:03:26 24 4
gpt4 key购买 nike

上下文:

根据Animation Spec :

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%from而不是声明属性关键帧。

  • B)0%from关键帧类中声明属性.


简化示例:

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 m(64 位)
  • 火狐 49.0.2
  • 边缘 25.10586.0.0

最佳答案

属性的计算值可能会有所不同,具体取决于应用于给定元素的 CSS 规则、这些规则的特殊性、元素是否具有该属性的内联样式声明、脚本是否正在修改该属性的值运行时的属性等。

如果您希望动画从一个可预测的固定值开始,您需要在 0% 关键帧中指定该值,这样它就不会从当时的计算值开始动画动画开始。

如果您可以保证在动画开始时该元素的计算值始终是一个常量值,则可以省略 0% 关键帧。如果您希望动画始终从当时的计算值开始(如果它可以更改),则必须省略 0% 关键帧。

类似的规则适用于 100% 关键帧:如果动画需要以固定值结束而不管计算值可能是什么,则需要指定 100% 关键帧;否则,如果它需要以与计算值相同的值结束,则需要将其省略。

关于css - 如果已经在应用动画的类中声明了某个属性,那么从 CSS 动画的 `0%` 关键帧中删除该属性是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40366905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com