我们以纯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%的时间,当然这些数值都可以根据自己的需要进行更改。
新人第一次发博客,如有不足之处还请各位大佬指出。