帮我用css写一个轮播图,3个图片,效果需求是渐变切换,间隔时间为2秒
时间: 2023-06-06 10:09:02 浏览: 236
好的,以下是对应的CSS代码:
HTML:
```html
<div class="slider">
<img src="1.jpg">
<img src="2.jpg">
<img src="3.jpg">
</div>
```
CSS:
```css
.slider {
position: relative;
height: 400px;
overflow: hidden;
}
.slider img {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 500ms ease-in-out;
z-index: -1;
}
.slider img:first-child {
opacity: 1;
z-index: 1;
}
.slider img.active {
opacity: 1;
z-index: 1;
}
@keyframes slider {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
.slider img:nth-child(2) {
animation-delay: 2s;
}
.slider img:nth-child(3) {
animation-delay: 4s;
}
.slider img.active:nth-child(1) {
animation: slider 6s infinite;
}
.slider img.active:nth-child(2) {
animation: slider 6s infinite;
}
.slider img.active:nth-child(3) {
animation: slider 6s infinite;
}
```
这个轮播图的原理是通过CSS的过渡和动画效果,将图片的透明度进行渐变切换,实现轮播的效果。其中,通过animation-delay属性设置了每张图片的切换时间(2秒),最后通过animation属性进行无限循环切换,实现了我们所需要的轮播效果。
阅读全文