简体   繁体   English

对CSS @keyframes规则进行分组

[英]Grouping CSS @keyframes rules

I completely understand that you cannot group animation keyframes selectors such as 我完全明白你不能将动画关键帧选择器组合起来

@keyframes,
@-moz-keyframes,
@-webkit-keyframes { /*do something*/ }

and that you absolutely MUST do 并且你绝对必须这样做

@keyframes { /*do something*/ }
@-moz-keyframes { /*do something*/ }
@-webkit-keyframes { /*do something*/ }

I know there are pre-processors that can do all this for me. 我知道有预处理器可以为我做这一切。 But I am more interested in the reason behind why this is the case? 但我更感兴趣的是为什么会出现这种情况的原因?

My google-fu is failing me. 我的google-fu让我失望了。 It seems to always direct me to a stackoverflow page telling someone they 'cannot' do it and they must separate them all out, or telling people about the pre-processors -or- I get sent to that horrible about.com and read stuff like 它似乎总是指向一个stackoverflow页面,告诉别人他们“不能”做到这一点,他们必须将它们全部分开,或者告诉人们关于预处理器 - 或者 - 我被发送到那个可怕的about.com并阅读像

Which obviously is not true in this case. 在这种情况下,这显然不正确。 If someone can direct me to an article, or explain to me why it cannot be grouped it would be most helpful. 如果有人可以指导我阅读一篇文章,或者向我解释为什么它不能分组,那将是最有帮助的。

Keep in mind that at-rules and selectors are completely different things. 请记住,规则和选择器是完全不同的东西。

At-rules are covered in this section of CSS2.1 spec , which says that an at-rule consists of exactly one at-keyword followed by some statement (be it a semicolon-terminated statement, or a block). CSS2.1规范的这一部分涵盖了规则 ,该规则指出一个at-rule只包含一个at-keyword,后跟一些语句(无论是以分号结尾的语句,还是一个块)。 As far as the CSS parser is concerned, what you have is a set of three separate at-rules, one prefix for each vendor and one unprefixed rule for the standard. 就CSS解析器而言,你所拥有的是一组三个独立的at-rules,每个供应商有一个前缀,标准有一个无前缀的规则。

The more appropriate counterpart to at-rules would be rule sets, or style rules, described here . 更合适的对手在规则是规则集,或样式规则,描述在这里 A rule set consists of a selector and a declaration block (containing style declarations). 规则集由选择器和声明块(包含样式声明)组成。 This is analogous to the contents of an at-rule as described above. 这类似于如上所述的规则的内容。 It also means that the selector is just one part of a rule set. 它还意味着选择器只是规则集的一部分

Certain at-rules do allow comma-separated values in their preludes, such as @media : 某些规则允许在其前奏中使用逗号分隔值,例如@media

@media screen, projection {
    /* Styles for both screen and projection media */
}

But instead of grouping the at-rules in entirety, the grouping happens within the value that comes after the at-keyword in the beginning of the rule. 但是,不是将整个规则分组,而是在规则开头的at-keyword之后的值内进行分组。

This @media rule can be expanded into two separate rules like so: 这个@media规则可以扩展为两个单独的规则,如下所示:

@media screen {
    /* Styles for screen media */
}

@media projection {
    /* Styles for projection media */
}

Notice that each rule has its own @media at-keyword. 请注意,每个规则都有自己的@media at-keyword。

Similarly, when you group multiple selectors into a single rule, what you have is one style rule . 同样,当您将多个选择器组合到一个规则中时,您拥有的是一种样式规则 The part that is grouped is the selector; 分组的部分是选择器; everything in the declaration block that follows applies to all the selectors that are listed in the group: 以下声明块中的所有内容都适用于组中列出的所有选择器:

.foo, .bar {
    /* Styles that apply to both .foo and .bar elements */
}

And when you expand it, it becomes two rule sets: 当你扩展它时,它变成两个规则集:

.foo {
    /* Styles that apply to .foo elements */
}

.bar {
    /* Styles that apply to .bar elements */
}

Because of 因为

When a user agent cannot parse the selector (ie, it is not valid CSS 2.1), it must ignore the selector and the following declaration block (if any) as well. 当用户代理无法解析选择器(即,它不是有效的CSS 2.1)时,它必须忽略选择器和以下声明块(如果有的话)。

found at http://www.w3.org/TR/CSS21/syndata.html#rule-sets http://www.w3.org/TR/CSS21/syndata.html#rule-sets


So each vendor prefix makes the whole rule un-parseable for all the other vendors. 因此,每个供应商前缀使整个规则对所有其他供应商都不可解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM