html - 如何将文本设置为在动画结束时居中对齐?

标签 html css animation

此代码为文本“Hello World!”播放动画,在动画开始时,文本颜色为红色。然后在 25% 时,它变为蓝色,在 50% 时,它变为居中对齐

但是当动画结束时,文本会移回起始位置并且不会居中对齐。

所以,这是我的代码:

p {
animation-name:sample;
animation-duration:3s;
}

@keyframes sample {
0% {color:red;}
50% {color:blue;}
75% {text-align:center;}
}

我的代码哪里出了问题?

最佳答案

您需要将animation-fill-mode 设置为forwards : https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode

forwards The target will retain the computed values set by the last keyframe encountered during execution. The last keyframe depends on the value of animation-direction and animation-iteration-count:

p {
animation-name:sample;
animation-duration:3s;
animation-fill-mode:   forwards ;
}

@keyframes sample {
0% {color:red;}
50% {color:blue;}
75%, 100% {text-align:center;}
}
<p>text to colorize then center</p>

text-align 不能是动画的,它会从一个值切换到另一个值。只有可以除的值才能逐位动画。

关于html - 如何将文本设置为在动画结束时居中对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253711/

相关文章:

javascript - 溢出 : hidden but i still want to have the empty scrollbar

javascript - AngularJS Scope.Feed 循环

css - 在一个 div 容器标签中居中多个嵌套的 div 标签

javascript - 一个css3/html5元素增长导致的空白如何填充?

javascript - 如何自动为 jquery 画廊制作动画

html - 如果父 div 尺寸较小,则给 div 滚动条

html - 居中覆盖顶部 div 底部的中间 div 内容和垂直堆叠的底部 div 顶部

html - 向导航项添加一个克拉,其类处于事件状态,不会将导航图标推出 div

animation - eb 指南中的 Sprite 动画(社区版)

ios - 宽度的 CALayer 动画