纯CSS3实现背景图淡入淡出轮播

本文介绍如何仅使用CSS3实现背景图片的淡入淡出效果,无需JavaScript,通过调整图片透明度(opacity)及背景图片URL实现平滑过渡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们以纯css3的方式来实现背景图淡入淡出,不设计js代码,首先我们来看一下思路,使用animation方法来实现这个循环的动画效果,淡入淡出效果我们通过设置opacity透明度来实现,最废话不多说,我们直接上代码。

html+css3代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
    .div_img{
        width: 100%;
        height: 100%;
        position: absolute;
        margin: 0 auto;
        top: 0;
        background: url(images/bg01.webp) ;
        background-repeat: no-repeat;
        background-size:cover ;
        opacity: 1;
        animation: fateimg 100s linear infinite;
    }
    @keyframes fateimg{
        0%{background: url(images/bg01.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        11%{background: url(images/bg01.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        12%{background: url(images/bg01.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        13%{background: url(images/bg02.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        23%{background: url(images/bg02.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        24%{background: url(images/bg02.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        25%{background: url(images/bg03.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        35%{background: url(images/bg03.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        36%{background: url(images/bg03.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        37%{background: url(images/bg04.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        47%{background: url(images/bg04.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        48%{background: url(images/bg04.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        49%{background: url(images/bg05.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        59%{background: url(images/bg05.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        60%{background: url(images/bg05.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        61%{background: url(images/bg06.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        72%{background: url(images/bg06.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        73%{background: url(images/bg06.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        74%{background: url(images/bg07.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        85%{background: url(images/bg07.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        86%{background: url(images/bg07.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        87%{background: url(images/bg08.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        98%{background: url(images/bg08.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
        99%{background: url(images/bg08.webp);opacity: 0;background-repeat: no-repeat;background-size:cover ;}
        100%{background: url(images/bg01.webp);opacity: 1;background-repeat: no-repeat;background-size:cover ;}
    }
        </style>
    </head>
    <body>
        <div class="div_img"></div>
    </body>
</html>

具体思路:

图片展示时间为11%,1%的淡入,1%的切换background url和淡出,具体来说就是1%时间保持本身的图片不切换,并且将opacity置为0,在完成后,在opacity为0的时候切换图片,并且opacity的值逐渐变换为1,并保持11%的时间,当然这些数值都可以根据自己的需要进行更改。

新人第一次发博客,如有不足之处还请各位大佬指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值