我试图创建一个图像交换仅使用css,我能够轻松地使图像交换的动画功能。但是,由于图像本身并不是完美的大小,并且使用带有更改背景的动画的div,并调整div的大小以适应整个屏幕:
width: 100%;
height: auto;对于图像,不能像通常用css完成的那样安装在屏幕上,如果它是用html img标记完成的,因此它会创建图像的各种副本来填充缺少的空间。
如何使动画将图像的宽度设置为100%?
.animation {
animation: imgswaper 10s infinite alternate;
width: 100%;
height: 300px;
}
@keyframes imgswaper {
0% {
background-image: url(https://www.collinsdictionary.com/images/full/apple_158989157.jpg);
}
1% {
background-image: url(https://www.collinsdictionary.com/images/full/apple_158989157.jpg);
}
50% {
background-image: url(https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?202207110316);
}
51%{
background-image: url(https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?202207110316);
}
100% {
background-image: url(https://www.collinsdictionary.com/images/full/apple_158989157.jpg);
}
}<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>Document</title>
</head>
<body>
<div class="animation"></div>
</body>
</html>
发布于 2022-09-07 00:16:17
添加背景大小和背景-重复到您的css。背景-图像只是离散的动画,尽管如此,它将看起来很简陋。
更多信息这里
.animation {
animation: imgswaper 10s infinite alternate;
width: 100%;
height: 300px;
background-size: contain;
background-repeat: no-repeat;
}
@keyframes imgswaper {
0% {
background-image: url(https://www.collinsdictionary.com/images/full/apple_158989157.jpg);
}
1% {
background-image: url(https://www.collinsdictionary.com/images/full/apple_158989157.jpg);
}
50% {
background-image: url(https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?202207110316);
}
51% {
background-image: url(https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?202207110316);
}
100% {
background-image: url(https://www.collinsdictionary.com/images/full/apple_158989157.jpg);
}
}<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>Document</title>
</head>
<body>
<div class="animation"></div>
</body>
</html>
https://stackoverflow.com/questions/73628283
复制相似问题