缩短 css3 动画关键帧定义
当我编写许多准相同的 CSS3 动画时,我想知道是否有办法缩短代码。
每次只有最终关键帧不同。
@-webkit-keyframes one {
from {
[identical properties]
}
[...etc...]
80% {
[identical properties]
}
to {
margin-left: 20px;
}
}
@-webkit-keyframes two {
from {
[identical properties]
}
[...etc...]
80% {
[identical properties]
}
to {
margin-left: 50px;
}
}
该代码非常长,所以我认为它可以很容易缩短,但看起来您必须一遍又一遍地定义整个动画。 我尝试过类似的方法,但在 Chrome 和 Safari 中不起作用。
@-webkit-keyframes one, two {
from {
[identical properties]
}
[...etc...]
80% {
[identical properties]
}
to {
margin-left: 20px;
}
}
@-webkit-keyframes two {
to {
margin-left: 50px;
}
}
有没有办法定义2个相同的动画? :o
While I was writing many quasi-identical CSS3 animations, I wondered if there's a way to shorten the code.
Each time, only the final keyframe is different.
@-webkit-keyframes one {
from {
[identical properties]
}
[...etc...]
80% {
[identical properties]
}
to {
margin-left: 20px;
}
}
@-webkit-keyframes two {
from {
[identical properties]
}
[...etc...]
80% {
[identical properties]
}
to {
margin-left: 50px;
}
}
That code is pretty long so I thought it could be easily shortened but looks like you have to define the whole animation over and over.
I tried something like this, but that doesn't work in Chrome and Safari.
@-webkit-keyframes one, two {
from {
[identical properties]
}
[...etc...]
80% {
[identical properties]
}
to {
margin-left: 20px;
}
}
@-webkit-keyframes two {
to {
margin-left: 50px;
}
}
Is there no way to define 2 identical animations? :o
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了避免在开发过程中复制粘贴,您可以使用 CSS 预处理器,例如 SCSS / SASS 和 更少。 mixin 功能大大减少了代码大小:
对于单个属性,编写单个 mixin 就足够了:
演示:一个繁忙的动画超市,使用纯 CSS(3)
对我来说,之前的 mixin 不够。我还想要动态名称和供应商前缀如何?,这样我就不必编写规则 10 x 5 = 50 次(10 个名称,5 个供应商)。这是展示 CSS 预处理器强大功能的机会:
To avoid copy-pasting during development, you can use a CSS preprocessor, such as SCSS / SASS and LESS. The mixin feature greatly reduces the code size:
For a single property, writing a single mixin is sufficient:
Demo: An animated busy supermarket, using pure CSS(3)
For me, the previous mixin was not sufficient. I also wanted dynamic names and vendor-prefixes how?, so that I do not have to write a rule 10 x 5 = 50 times (10 names, 5 vendors). That's a chance to show the power of a CSS preprocessor:
目前还没有。请记住,如果您对 CSS 进行 gzip 压缩,那么许多低效现象将会消失。
Not at the moment. Remember that if you are gzipping your CSS that a lot of this inefficiency will disappear.
不是天生的。特别是对于供应商前缀,CSS 可能会变得非常令人眼花缭乱,但是如果您正确部署文件(GZip、缓存等),这并不是效率低下,只是编写起来很痛苦。
根据您的项目,您可以 将 CSS 解析为 PHP 并在那里定义变量。我发现这个想法真的很性感,但还没有足够的需要使用它。
Not inherently. Especially with vendor prefixes, CSS can get really dizzying, however if you are deploying the file properly (GZip, caching, etc.) it's not really an inefficiency, just a pain in the butt to write.
Depending on your project, you can parse CSS as PHP and define variables there. I find the idea really sexy but haven't had enough of a need to use it.