【问题标题】:How to get rid of the white flash when changing background image with animation?用动画改变背景图像时如何摆脱白色闪光?
【发布时间】:2018-02-13 08:34:05
【问题描述】:
代码使网站的背景图像在 5 个不同的图像之间转换,但每次动画完成并且下一个动画开始时都会出现白色闪光。我该如何摆脱它或我做错了什么?不知道怎么解释。
@keyframes backswitch {
0% {
background-image: url("background.jpg");
}
20% {
background-image: url("background_2.jpg");
}
40% {
background-image: url("background_3.jpg");
}
60% {
background-image: url("background_4.jpg");
}
80% {
background-image: url("background_5.jpg");
}
100% {
background-image: url("background_6.jpg");
}
}
body {
/*Adjusting images and animation*/
background-repeat: no-repeat;
max-width: 100%;
max-height: 100%;
background-size: cover;
animation-name: backswitch;
animation-duration: 60s;
animation-iteration-count: infinite;
}
<!DOCTYPE html>
<html>
<body>
</body>
</html>
【问题讨论】:
标签:
css
html
css-animations
【解决方案1】:
您必须预加载图片:
@keyframes backswitch {
0% {background-image: url("https://dummyimage.com/300/ccc/fff.png");}
20% {background-image: url("https://dummyimage.com/300/3f5/fff.png");}
40% {background-image: url("https://dummyimage.com/300/71c/fff.png");}
60% {background-image: url("https://dummyimage.com/300/228/fff.png");}
80% {background-image: url("https://dummyimage.com/300/c11/fff.png");}
100% {background-image: url("https://dummyimage.com/300/544/fff.png");}
}
body {
/*Adjusting images and animation*/
background-repeat: no-repeat;
max-width: 100%;
max-height: 100%;
background-size: cover;
animation-name: backswitch;
animation-duration: 60s;
animation-iteration-count: infinite;
}
div.preload-images {
background: url("https://dummyimage.com/300/ccc/fff.png") no-repeat -9999px -9999px;
background: url("https://dummyimage.com/300/ccc/fff.png") no-repeat -9999px -9999px,
url("https://dummyimage.com/300/3f5/fff.png") no-repeat -9999px -9999px,
url("https://dummyimage.com/300/71c/fff.png") no-repeat -9999px -9999px,
url("https://dummyimage.com/300/228/fff.png") no-repeat -9999px -9999px,
url("https://dummyimage.com/300/c11/fff.png") no-repeat -9999px -9999px,
url("https://dummyimage.com/300/544/fff.png") no-repeat -9999px -9999px;
}
<body>
<div class="preload-images"></div>
</body>
【解决方案2】:
如果您将 background-image 属性分配给正文,您可以预加载所有图像,这个问题应该会消失。
body {
background-image: url("background_1.jpg"), url("background_2.jpg"), url("background_3.jpg"), url("background_4.jpg"), url("background_5.jpg"), url("background_6.jpg");
background-repeat: no-repeat;
max-width: 100%;
max-height: 100%;
background-size: cover;
animation-name: backswitch;
animation-duration: 60s;
animation-iteration-count: infinite;
}
@keyframes backswitch {
0% {
background-image: url("background.jpg");
}
20% {
background-image: url("background_2.jpg");
}
40% {
background-image: url("background_3.jpg");
}
60% {
background-image: url("background_4.jpg");
}
80% {
background-image: url("background_5.jpg");
}
100% {
background-image: url("background_6.jpg");
}
}
【解决方案3】:
我检查了标准图像(相对于背景图像)的行为。
他们似乎没有受到这个问题的困扰,至少在 Firefox 或 Chrome 中没有。
因此,您可以将顶层内容放在 z-index 1 的相对 div 中,然后将图像放在 z-index 0 的绝对 div 中。
应该不会花很长时间来更新你的 CSS 并让一切都很好地适应。