我正在尝试使用css3自动更改背景图像。我有9张图片要在后台更改。我面临的问题是,它只显示滑动的第一个和最后一个图像,但不显示2,3,4,5,6,7,8。我有以下代码:
<img id="img1" src="images/1.gif">
<img id="img2" src="images/2.gif">
<img id="img3" src="images/3.gif">
<img id="img4" src="images/4.gif">
<img id="img5" src="images/5.gif">
<img id="img6" src="images/6.gif">
<img id="img7" src="images/7.gif">
<img id="img8" src="images/8.gif">
<img id="img9" src="images/9.gif">
这是CSS:
#img1, #img2, #img3, #img4, #img5, #img6, #img7, #img8, #img9 {
width:610px;
height:610px;
position:scroll;
z-index:-1;
animation-name: test;
animation-duration: 90s;
opacity:0;
}
#img2 {
animation-delay:10s;
-webkit-animation-delay:10s
}
#img3 {
animation-delay:20s;
-webkit-animation-delay:20s
}
#img4 {
animation-delay:30s;
-webkit-animation-delay:30s
}
#img5 {
animation-delay:40s;
-webkit-animation-delay:40s
}
#img6 {
animation-delay:50s;
-webkit-animation-delay:50s
}
#img7 {
animation-delay:60s;
-webkit-animation-delay:60s
}
#img8 {
animation-delay:70s;
-webkit-animation-delay:70s
}
#img9 {
animation-delay:80s;
-webkit-animation-delay:80s
}
@-webkit-keyframes test {
0% {
opacity: 1;
}
50% {
opacity: 1;
}
100% {
opacity: 1;
}
}
@keyframes test {
0% {
opacity: 1;
}
50% {
opacity: 1;
}
100% {
opacity: 1;
}
}
每张幻灯片从1到8的延迟时间是10秒,但image9除外,延迟时间为8.60秒。
请在我出错的地方帮忙。 :(
答案 0 :(得分:5)
好的,我已经制作了一个JS小提琴,我想我已经想出了你想要的东西。
http://jsfiddle.net/d48vdxmv/2/
基本上,您需要先将图像包装在容器中:
<div id="container">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_01.jpg">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_02.jpg">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_03.jpg">
<img src="http://imaging.nikon.com/lineup/dslr/df/img/sample/img_04.jpg">
</div>
然后在CSS中,将容器设置为相对位置,并将图像设置为绝对,以便它们彼此叠加。
#container {
position:relative;
height:610px;
width:610px;
}
#container img {
position:absolute;
left:0;
}
然后使用不同的浏览器变体添加关键帧
@-webkit-keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}
@-moz-keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}
@-o-keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}
@keyframes imgFade {
0% {
opacity:1;
}
17% {
opacity:1;
}
25% {
opacity:0;
}
92% {
opacity:0;
}
100% {
opacity:1;
}
}
然后是所有浏览器的动画详细信息
#container img {
-webkit-animation-name: imgFade;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 8s;
-moz-animation-name: imgFade;
-moz-animation-timing-function: ease-in-out;
-moz-animation-iteration-count: infinite;
-moz-animation-duration: 8s;
-o-animation-name: imgFade;
-o-animation-timing-function: ease-in-out;
-o-animation-iteration-count: infinite;
-o-animation-duration: 8s;
animation-name: imgFade;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 8s;
}
最后是每张图片的持续时间
#container img:nth-of-type(1) {
-webkit-animation-delay: 6s;
-moz-animation-delay: 6s;
-o-animation-delay: 6s;
animation-delay: 6s;
}
#container img:nth-of-type(2) {
-webkit-animation-delay: 4s;
-moz-animation-delay: 4s;
-o-animation-delay: 4s;
animation-delay: 4s;
}
#container img:nth-of-type(3) {
-webkit-animation-delay: 2s;
-moz-animation-delay: 2s;
-o-animation-delay: 2s;
animation-delay: 2s;
}
#container img:nth-of-type(4) {
-webkit-animation-delay: 0;
-moz-animation-delay: 0;
-o-animation-delay: 0;
animation-delay: 0;
}