html - 显示动画
问题描述
我编写了一个显示图像动画,并在创建时偶然发现了一个问题。
.container{
overflow: hidden;
}
.image {
background-image: url('https://images.wallpaperscraft.com/image/honda_civic_type_r_honda_type_r_honda_129270_3840x2160.jpg');
background-size: cover;
width: 400px;
height: 400px;
animation: 1s ease-out 0s 1 slideInFromLeft;
overflow: hidden;
}
@keyframes slideInFromLeft {
0% {
transform: translatex(-100%);
}
100% {
transform: translatex(0%);
}
}
<div class="container">
<div class="image">
</div>
</div>
除了滑动不顺畅之外,动画似乎运行良好。它似乎有某种抖动。
如何使动画幻灯片更流畅?
解决方案
您可以等待页面/图像加载,然后触发动画发生。下面的代码是用js应用一次动画类的粗略方式。
window.onload = function() {
var element = document.getElementById('img1');
element.classList.add("show");
element.classList.add("animate");
}
.container{
overflow: hidden;
}
.image {
background-image: url('https://images.wallpaperscraft.com/image/honda_civic_type_r_honda_type_r_honda_129270_3840x2160.jpg');
background-size: cover;
width: 400px;
height: 400px;
opacity: 0;
overflow: hidden;
}
#img1.animate{
animation: 1s ease-out 0s 1 slideInFromLeft;
}
.show {
opacity:1;
}
@keyframes slideInFromLeft {
0% {
transform: translatex(-100%);
}
100% {
transform: translatex(0%);
}
}
<div class="container">
<div id="img1" class="image" >
</div>
</div>
推荐阅读
- c++ - C ++有一个函数在某个时间激活而不会中断
- c# - 如何在 Kentico API 中赋予角色创建和删除节点的权限?
- ruby - 为什么数组在全名枚举期间返回一个随机布尔值,而不是在 ruby 控制台中?irb
- c# - 值对象的 DDD 嵌套
- android - io.realm.exceptions.RealmException:Dog 不是此 Realm 架构的一部分
- docker - 可以从其他容器访问 docker 容器,但不能从 localhost
- utf-8 - Dgraph - 无法存储表情符号
- excel - 选择范围取决于两个关键词
- r - 评估闪亮的应用程序输入是否不为空不工作,如何做对?
- html - 如何将 div 对齐到页面的右侧