css - 在纯 CSS 中为元素添加图像背景大小动画效果
问题描述
为什么我无法在加载时添加更改 div 的背景图像大小的动画?我知道使用 JS 可以通过向 body 或元素添加一个类来实现这一点,但是我们如何在纯 CSS 中实现这一点?
html,body {
height: 100%;
width:100%;
}
div {
height: 100%;
width:100%;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/d/da/Internet2.jpg);
background-size: 100%;
background-repeat: no-repeat;
background-position: 50% 50%;
animation-name: animate;
transition: all 3s;
}
.animate {
from {background-size: 100%}
to {background-size: 110%}
}
<div></div>
解决方案
html, body { height: 100%; width: 100%; }
div {
height: 100%;
width: 100%;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/d/da/Internet2.jpg);
background-size: 100%;
background-repeat: no-repeat;
background-position: 50% 50%;
animation: animate 3s ease forwards;
transition: all 3s;
}
@keyframes animate {
from { background-size: 100%; }
to { background-size: 150%; }
}
<div></div>
推荐阅读
- ssl - Windows 10 设备门户预配自签名 SSL 证书
- r - 用 R 中的向量替换 data.frame 的一些值
- ios - Receigen Xcode 10.1 - 未知类型名称'UIDevice'
- node.js - 浏览器/防病毒软件可以阻止对直接 IP 地址而不是域名的请求吗?
- c++ - 功能定义不允许 RetailItem
- dialogflow-es - 使用 Dialogflow 时,后续意图与在父意图中需要参数有什么区别?
- kotlin - 如何在 Workspace 标题中创建搜索字段?
- c++11 - 将节点(包含 str 和 bool 值)插入数组 C++
- sql - 将列名声明为变量
- java - 使用 Jackson GWT 访问嵌套的 JSON 值