css - 如何正确更改此代码?
问题描述
这是我第一次想使用滑动图像,我在 codepen 上找到了完美的幻灯片,但无法管理它只有 2 或 3 张图像可以滑动。找不到,如何正确设置时间。如果我只需要 2 或 3 张图像在每张图像上 4 秒的时间内滑动,它是如何工作的?
@import url('https://fonts.googleapis.com/css?family=Bubblegum+Sans|Ubuntu');
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
font-family: 'Bubblegum Sans', cursive;
}
slider {
display: block;
width: 100%;
height: 100%;
background-color: #1f1f1f;
overflow: hidden;
position: absolute;
}
slider > * {
position: absolute;
display: block;
width: 100%;
height: 100%;
background: #1f1f1f;
animation: slide 12s infinite;
overflow: hidden;
}
slide:nth-child(1) {
left: 0;
z-index: 9;
animation-delay: -1s;
background-image: url(https://preview.ibb.co/fm57uG/img_1.jpg);
background-size: cover;
background-position: center;
}
slide:nth-child(2) {
animation-delay: 2s;
background-image: url(https://preview.ibb.co/kkT5gw/img_2.jpg);
z-index: 8;
background-size: cover;
background-position: center;
}
slide:nth-child(3) {
animation-delay: 5s;
background-image: url(https://preview.ibb.co/dVT5gw/img_3.jpg);
background-size: cover;
background-position: center;
z-index: 7;
}
slide:nth-child(4) {
left: 0%;
animation-delay: 8s;
background-image: url(https://preview.ibb.co/m7FU8b/img_4.jpg);
background-size: cover;
background-position: center;
z-index: 6;
}
slide p {
font-size: 70px;
text-align: center;
display: inline-block;
width: 100%;
margin-top: 340px;
color: #fff;
}
@keyframes slide {
0% {
left: 100%;
width: 100%;
}
5% {
left: 0%;
}
25% {
left: 0%;
}
30% {
left: -100%;
width: 100%;
}
30.001% {
left: -100%;
width: 0%;
}
100% {
left: 100%;
width: 0%;
}
}
<slider>
<slide>
<p>Slide 1</p>
</slide>
<slide>
<p>Slide 2</p>
</slide>
<slide>
<p>Slide 3</p>
</slide>
<slide>
<p>Slide 4</p>
</slide>
</slider>
提前致谢!
解决方案
3张幻灯片的示例:
HTML
<slider>
<slide>
<p>Slide 1</p>
</slide>
<slide>
<p>Slide 2</p>
</slide>
<slide>
<p>Slide 3</p>
</slide>
</slider>
CSS
slide:nth-child(1) {
left: 0;
z-index: 9;
animation-delay: -1s;
background-image: url(https://preview.ibb.co/fm57uG/img_1.jpg);
background-size: cover;
background-position: center;
}
slide:nth-child(2) {
animation-delay: 3s;
background-image: url(https://preview.ibb.co/kkT5gw/img_2.jpg);
z-index: 8;
background-size: cover;
background-position: center;
}
slide:nth-child(3) {
animation-delay: 7s;
background-image: url(https://preview.ibb.co/dVT5gw/img_3.jpg);
background-size: cover;
background-position: center;
z-index: 7;
}
诀窍是遵循动画延迟。第一张幻灯片立即开始(-1s),第二张幻灯片添加 4 秒,得到(3s),第三张幻灯片再次添加 4s(7s),依此类推。如果您需要 2 张幻灯片,请从 HTML 中删除第三张幻灯片并从 CSS 中删除第三个子项。
注意:我只为幻灯片的孩子放 CSS,你确保你也有其余的。
推荐阅读
- html - 浮动不起作用,所以导航不能水平
- javascript - Insert 500k records in mongodb one by one causes heap out of memory
- javascript - 如何在页面加载时用 JS 或 jQuery 将纯文本替换为具有相同文本但在其周围标记的特定值
- c++ - 当代码在 Visual Studio C++ 中正常工作时出现 MSB6006 错误
- html - Bootstrap 4 - 如何为不同的屏幕尺寸重新定位一些导航栏元素
- mockito - Mockito 多部分文件参数匹配器
- javascript - Webpack - 代码拆分:将库拆分为自己的文件
- reactjs - 字段值未定义
- amazon-web-services - AWS Glue NameError:未定义名称“DynamicFrame”
- python - 声明变量的内存与在函数调用中定义时的对比