首页前端开发CSScss如何实现图片轮播代码怎么写

css如何实现图片轮播代码怎么写

时间2023-11-27 08:50:03发布访客分类CSS浏览1002
导读:CSS中实现图片轮播可以使用以下几种方法:1. 使用CSS3的animation动画.carousel { animation: carousel 5s infinite;}@keyframes carousel { 0% {...

CSS中实现图片轮播可以使用以下几种方法:

1. 使用CSS3的animation动画

.carousel {
        animation: carousel 5s infinite;
}
@keyframes carousel {
    0% {
            opacity: 0;
            transform: translateX(0%);
    }
    20% {
            opacity: 1;
    }
    80% {
            opacity: 1;
    }
    100% {
            opacity: 0;
            transform: translateX(-100%);
    }
}

2. 使用CSS3的transition动画

.carousel {
        position: relative;
        overflow: hidden;
        display: flex;
}
.carousel img {
        flex: 1 0 100%;
        transition: transform 1s ease;
}
.carousel img:last-child {
        position: absolute;
        left: 100%;
}
.carousel:hover img {
        transform: translateX(-25%);
}
.carousel:hover img:last-child {
        transform: translateX(-125%);
        transition-delay: 0.5s;
}

3. 使用CSS的定位和动画

.carousel {
        position: relative;
        overflow: hidden;
}
.carousel img {
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
        transition: opacity 1s ease;
}
.carousel img:first-child {
        opacity: 1;
        z-index: 1;
}
.carousel:hover img:first-child {
        opacity: 0;
}
.carousel:hover img:nth-child(2) {
        opacity: 1;
        z-index: 1;
        animation: slide 1s ease;
}
.carousel:hover img:nth-child(3) {
        opacity: 1;
        z-index: 2;
        animation: slide 1s 0.5s ease;
}
.carousel:hover img:nth-child(4) {
        opacity: 1;
        z-index: 3;
        animation: slide 1s 1s ease;
}
@keyframes slide {
    from {
            transform: translateX(100%);
    }
    to {
            transform: translateX(0%);
    }
}
    

通过以上代码例子,可以看出CSS实现图片轮播有多种方式,开发者可以根据自己的需求和喜好进行选择。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: css如何实现图片轮播代码怎么写
本文地址: https://pptw.com/jishu/557333.html
css3 hover 显示隐藏 css如何实现写轮眼

游客 回复需填写必要信息