首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将宽度100%设置为动画中的图像

将宽度100%设置为动画中的图像
EN

Stack Overflow用户
提问于 2022-09-06 22:17:46
回答 1查看 28关注 0票数 0

我试图创建一个图像交换仅使用css,我能够轻松地使图像交换的动画功能。但是,由于图像本身并不是完美的大小,并且使用带有更改背景的动画的div,并调整div的大小以适应整个屏幕:

代码语言:javascript
复制
width: 100%;
height: auto;

对于图像,不能像通常用css完成的那样安装在屏幕上,如果它是用html img标记完成的,因此它会创建图像的各种副本来填充缺少的空间。

如何使动画将图像的宽度设置为100%?

代码语言:javascript
复制
.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);
    }
}
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="style.css" />
    <title>Document</title>
  </head>
  <body>
    <div class="animation"></div>
  </body>
</html>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-07 00:16:17

添加背景大小和背景-重复到您的css。背景-图像只是离散的动画,尽管如此,它将看起来很简陋。

更多信息这里

代码语言:javascript
复制
.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);
  }
}
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" href="style.css" />
  <title>Document</title>
</head>

<body>
  <div class="animation"></div>
</body>

</html>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73628283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档