您需要为此使用关键帧动画- DEMO
HTML:
<img src='http://imgsrc.hubblesite.org/hu/db/images/hs-2012-10-a-web.jpg'>
<img src='http://imgsrc.hubblesite.org/hu/db/images/hs-2004-45-a-web.jpg'>
CSS:
img {
position: absolute;
width : 320px;
height: 180px;
}
img:last-child { animation: fader 4s infinite alternate; }
@keyframes fader { to { opacity: 0; } }
编辑
如果您的图像具有透明度,那么您需要为它们两个的不透明度设置动画,而不仅仅是顶部的那个。像这样 -演示
img {
opacity: 0;
position: absolute;
width : 256px;
height: 256px;
}
img:first-child { animation: fadein 8s infinite alternate; }
img:last-child { opacity: 1; animation: fadeout 8s infinite alternate; }
@keyframes fadein { 50% { opacity: 1; } }
@keyframes fadeout { 50% { opacity: 0; } }
另外,请记住,您必须使用前缀(我没有使用任何前缀,因为 dabblet 包含 -prefix-free 并且这样更容易突出这个想法):
img:first-child {
-webkit-animation: fadein 8s infinite alternate; /* Chrome, Safari, Android, Blackberry */
-moz-animation: fadein 8s infinite alternate; /* FF, FF for Android */
-o-animation: fadein 8s infinite alternate; /* Opera 12 */
animation: fadein 8s infinite alternate; /* IE 10, FF 16+, Opera 12.5 */
}
@-webkit-keyframes fadein { 50% { opacity: 1; } }
@-moz-keyframes fadein { 50% { opacity: 1; } }
@-o-keyframes fadein { 50% { opacity: 1; } }
@keyframes fadein { 50% { opacity: 1; } }
/* same for the other set (fadeout) */