背景动画不起作用(CSS,SCSS)



我希望我的背景能缓慢而渐进地出现,我认为这会很容易,但我做不到。有什么建议吗?我只能用html、CSS和SCSS(还不是后者的熟练用户(来做吗。

body {
background-image: url(https://images.pexels.com/photos/1001682/pexels-photo-1001682.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
background-repeat: no-repeat;
animation-name: b-anime;
animation-delay: 1s;
animation-duration: 4s;
animation-timing-function: ease-in-out;
animation-fill-mode: forwards;
transition-property: background;
@keyframes b-anime {
0% {
opacity: 0;
}
25% {
opacity: 0.25;
}
50% {
opacity: 0.5;
}
75% {
opacity: 0.75;
}
100% {
opacity: 1;
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>icy-blues</title>
<link rel="stylesheet" type="text/css" href="website1.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width>
<meta name=" description " content="xx ">
<meta name="keywords " content="stuff ">
<meta name="author " content="Ray ">
</head>
<body>
</body>
</html>

首先,您的keyframes定义必须在body样式之外。然后,在这种情况下,不可能将opacity用于background-image透明度,请查看不透明度文档。

但是,您可以为::after元素设置透明效果,并为此设置动画。

你可以这样做:

body {
position: relative;
height: 100vh;
width: 100vw;
padding: 0;
margin: 0;
}
body::after {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
background-image: url(https://images.pexels.com/photos/1001682/pexels-photo-1001682.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
background-repeat: no-repeat;
background-position: center;
width: 100%;
height: 100%;
opacity: 0;
animation-name: b-anime;
animation-delay: 1s;
animation-duration: 4s;
animation-timing-function: ease-in-out;
animation-fill-mode: forwards;
transition-property: background;
}
@keyframes b-anime {
0% {
opacity: 0;
}
25% {
opacity: 0.25;
}
50% {
opacity: 0.5;
}
75% {
opacity: 0.75;
}
100% {
opacity: 1;
}
}
<body></body>

最新更新