html - CSS动画自定义属性/变量
问题描述
我一直试图让它工作一段时间。
关键是内部 div 会有一些形状,而且可能不止一个(这就是我使用nth-child
选择器的原因)。这个内部 div 应该被显示,然后在一段时间内再次隐藏。问题是,我想在一个动画中为所有(后来的)多个内部 div 设置动画。为此,我认为我可以使用 CSS 变量,但这似乎不起作用。
在这个例子中我试图归档的是内部 div 基本上只是通过使用变量来闪烁。但我在 Firefox 中的结果只是一个黑匣子。
我错过了什么吗?我已经查过是否可以在其中使用 CSS 变量,@keyframes
并且确实可以。它们在动画中的唯一问题似乎是它们没有在两者之间进行插值,而是它们突然切换,在这种情况下这不是问题。
@keyframes test{
from{
--one: 0;
}
to{
--one: 1;
}
}
#test{
width: 100px;
height: 200px;
background-color: black;
animation: test 1s infinite;
}
#test :nth-child(1){
width: 20px;
height: 20px;
margin: auto;
background-color: white;
opacity: var(--one,0);
}
<div id="test">
<div></div>
</div>
解决方案
这可以通过使用定义变量来实现(截至撰写本文时,还没有得到很好的支持)@property
,它允许声明类型并允许浏览器“理解”,例如,某个属性(变量)是一个数字,然后它可以逐渐动画/过渡该变量。
示例代码:
@property --opacity {
syntax: '<number>'; /* <- defined as type number for the transition to work */
initial-value: 0;
inherits: false;
}
@keyframes fadeIn {
50% {--opacity: 1}
}
html {
animation: 2s fadeIn infinite;
background: rgba(0 0 0 / var(--opacity));
}
当前允许的类型包括:
length
, number
, percentage
, length-percentage
, color
, image
, url
, integer
, angle
, time
, resolution
, transform-list
, transform-function
, custom-ident
(标识符字符串)
有用的文章:
推荐阅读
- java - 使用jni4net在java中使用Native DLL函数并得到System.BadImageFormatException
- python - Python 元组赋值与列表追加
- javascript - 已修复:Wordpress 站点未捕获类型错误:jQuery(...).live 不是导致带有 JS 的图像不显示的函数
- terraform - 将 keyvault 导入 terraform 后如何添加 keyvault 访问策略
- c++ - 为什么为 ifstream 指定 std::ios::in?
- javascript - jQuery获取多个选择列表中最后选择的选项的值
- python - 如何检查连续相同的值和值的计数同时出现熊猫
- autodesk-forge - Forge 设计自动化 API (AutoCAD) 接受哪些输入文件类型?
- spring - 如何在春季动态执行验证?
- android - 如何测试自定义视图性能