在循环中使用 CSS 的图像之间的淡入淡出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17954874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cross-Fade between images with CSS in loop
提问by Rachit Sharma
I want to fade between images in a loop (like result here-jsfiddle.net/5M2PD) but purely through CSS
, no JavaScript
. I tried using key-frames but I wasn't successful. Please Help.
我想在一个循环中的图像之间淡入淡出(就像这里的结果-jsfiddle.net/5M2PD),但纯粹是通过CSS
,没有JavaScript
。我尝试使用关键帧,但没有成功。请帮忙。
@keyframes cf3FadeInOut {
0% {
opacity:1;
}
45% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}
#cf3 img.top {
animation-name: cf3FadeInOut;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 10s;
animation-direction: alternate;
}
回答by vals
I have taken your fiddle as a base, and made it work without script.
我已经把你的小提琴作为基础,让它在没有脚本的情况下工作。
updated demo
更新演示
I needed to set an id to the HTML
我需要为 HTML 设置一个 id
<div class="fadein">
<img id="f3" src="http://i.imgur.com/R7A9JXc.png">
<img id="f2" src="http://i.imgur.com/D5yaJeW.png">
<img id="f1" src="http://i.imgur.com/EUqZ1Er.png">
</div>
And the CSS is:
CSS是:
.fadein img {
position:absolute;
left:-65px;
top:0;
-webkit-animation-name: fade;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 6s;
animation-name: fade;
animation-iteration-count: infinite;
animation-duration: 6s;
}
@-webkit-keyframes fade {
0% {opacity: 0;}
20% {opacity: 1;}
33% {opacity: 1;}
53% {opacity: 0;}
100% {opacity: 0;}
}
@keyframes fade {
0% {opacity: 0;}
20% {opacity: 1;}
33% {opacity: 1;}
53% {opacity: 0;}
100% {opacity: 0;}
}
#f1 {
background-color: lightblue;
}
#f2 {
-webkit-animation-delay: -4s;
background-color: yellow;
}
#f3 {
-webkit-animation-delay: -2s;
background-color: lightgreen;
}
I am setting the keyframes to give aprox 1/3 of the time visible, with apropiate transitions. Then I set different delays for every image, so that they alternate. If you want full browser support, you will need more vendor prefixes. I have used -webkit- and bare property so that you get the idea.
我正在设置关键帧以提供大约 1/3 的可见时间,并具有适当的过渡。然后我为每个图像设置不同的延迟,以便它们交替。如果您想要完整的浏览器支持,您将需要更多供应商前缀。我已经使用了 -webkit- 和裸属性,以便您了解这个想法。