【发布时间】:2019-04-02 05:29:59
【问题描述】:
已经有一段时间了。无论如何,在制作动画时 让一个绝对定位的元素保持居中有些麻烦。更重要的是我在动画完成后看到元素“移位”,我不知道为什么。我发现的最接近的问题是css animations moves element position,但这并不能解决这个常见的用例。
调试动画帧,我们可以看到:
呸!玩定位没用,但是animation-fill-mode做了点什么;但是,我注意到translate 在动画期间或animation-fill-mode 设置为forwards 或both 时对定位元素没有影响。为什么?副作用是当动画完成时,元素的位置不会被重新计算,因此不会重新绘制。
例子:
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
以及使用 animate.css 的 CSS 让生活更轻松:
.alert {
min-width: 500px;
position: absolute;
top: 0;
left: 50%;
animation-name: fadeInDown;
animation-duration: 0.75s;
animation-timing-function: ease-in-out;
/* animation-fill-mode: forwards; */
transform: translateX(-50%);
}
演示:https://codepen.io/atomicpages/pen/yrymVY?editors=1100
目标:
- 居中对齐
- 元素至少
500px - 动画帧完成后元素不移动
感谢您的帮助。
【问题讨论】:
-
不,我们在代码示例中提供的约束范围内操作。
标签: html css animation css-animations