html - 我不知道为什么这个动画有效
问题描述
如果 #box 中不存在“位置:绝对”,为什么动画不起作用?
当“位置:绝对”在#box中删除它时,我尝试了此代码,但它不起作用。
<style>
#box{
**position:absolute;**
width:200px;
height:200px;
background-color:red;
animation:animate 2s none infinite alternate;
}
@keyframes animate{
from{
left:0;
}
50%{
left:500px;
}
to{
left:500px;
}
}
</style>
解决方案
您不应该为左/右/上/下设置动画,最好使用transforms。变换允许元素在视觉上移动,而它们在 DOM 上的空间仍然存在,从而使动画更安全/更流畅。
试试这个
#box {
width: 200px;
height: 200px;
background-color: red;
animation: animate 2s linear forwards infinite alternate;
}
@keyframes animate {
0% {
transform: translateX(0px);
}
100% {
transform: translateX(100px);
}
}
工作演示jsfiddle
推荐阅读
- f# - 如何在 F# 中求和和赋值?
- node.js - 将内存存储到 @google-cloud/storage 未完成
- asp.net-core - 带有列表的自定义 Json.Net 转换器
- python - 如何从python中的查询结果中删除“小数”
- php - Laravel $collection->key = "value" 不起作用
- php - 如何将每个 foreach 迭代发送到 PHP 中的 json 有效负载
- php - 我将如何在视图中编写 Laravel ifelse 条件
- mysql - solr 8.5.1 无法加载驱动程序:com.mysql.jdbc.Driver
- php - 如何在成功付款 authorize.net SIM 方法上获取订单 ID 或任何参考 ID
- ios - 循环遍历嵌套字典以连接 Napalm 中的多个供应商设备