css - 带淡入的 CSS 图像滑块
问题描述
我一直在尝试创建一个简单的图像滑块,展示一个网站的四个图像。我已经设法创建了幻灯片,但是每个图像之间的过渡非常快,我想要一点淡入效果,以便它更平滑。这里的很多问题已经建议使用 jQuery,但我正在尝试只使用 CSS。我也探索了 animate.css 但无法让它工作。感谢提供的任何/所有帮助。谢谢 :)
到目前为止的代码如下:
HTML
<div class="slider">
<div class="feature">
</div>
<div class="overlay">
</div>
</div>
和 CSS
.feature {
animation: slide 3s;
}
.slider {
background-repeat: no-repeat;
background-size: cover;
width: 100%;
height: 80vh;
animation: slide 10s infinite;
}
.overlay {
color: #fff;
width: 100%;
height: 80vh;
background-color: rgba(0, 0, 0, 0.5);
}
@keyframes slide {
0%{
background-image: url(../resources/feature/Feature1.jpg);
}
25%{
background-image: url(../resources/feature/Feature1.jpg);
}
25.1%{
background-image: url(../resources/feature/Feature2.jpg);
}
50%{
background-image: url(../resources/feature/Feature2.jpg);
}
50.01%{
background-image: url(../resources/feature/Feature3.jpg);
}
75%{
background-image: url(../resources/feature/Feature3.jpg);
}
75.01%{
background-image: url(../resources/feature/Feature4.jpg);
}
100%{
background-image: url(../resources/feature/Feature4.jpg);
}
}
解决方案
您必须设置“滑块”的opacity
andtransition
才能获得效果。
.feature {
}
.slider {
background-repeat: no-repeat;
background-size: cover;
width: 100%;
height: 80vh;
transition: all .2s ease-in-out;
animation: slide 10s infinite;
}
.overlay {
color: #fff;
width: 100%;
height: 80vh;
background-color: rgba(0, 0, 0, 0.5);
transition: all .2s ease-in-out;
}
@keyframes slide {
0%{
opacity: 0;
background-color: red;
}
20%{
opacity: 1;
background-color: red;
}
25%{
opacity: 0;
background-color: red;
}
25.1%{
opacity: 0;
background-color: blue;
}
45%{
opacity: 1;
background-color: blue;
}
50%{
opacity: 0;
background-color: blue;
}
50.01%{
opacity: 0;
background-color: yellow;
}
70%{
opacity: 1;
background-color: yellow;
}
75%{
opacity: 0;
background-color: yellow;
}
75.01%{
opacity: 0;
background-color: green;
}
95%{
opacity: 1;
background-color: green;
}
100%{
opacity: 0;
background-color: green;
}
}
<div class="slider">
<div class="feature">
</div>
<div class="overlay">
</div>
</div>
推荐阅读
- sql - mssql 7.0 无法识别空数据库条目
- c# - Vector.moveTowards 的平滑动画不起作用?
- java - 我应该在我的应用程序上使用什么 OAuth 2 工作流程
- oracle - oracle apex 问题用sigh 怎么解决?
- node.js - 登录身份验证不起作用或在 nodejs 中验证用户
- vhdl - 如何使用 vhdl 找到具有最高值的数组索引?
- sql - 从两个表中透视数据
- omnet++ - 如何在 MAC 层获取发送方位置
- python - Split UpperCamelCase and UPPERCamelCase to separate words
- google-sheets - 在 REGEXMATCH 中将 TRUE 或 FALSE 转换为 Yes 或 No