简体   繁体   English

供应商前缀中的上部驼峰案例与较低的驼峰案例

[英]Upper camel case vs. lower camel case in vendor prefixes

The following both seem to be working: 以下两者似乎都有效:

element.style.WebkitFlex = 1;
element.style.webkitFlex = 1;

But which syntax is standard? 但哪种语法是标​​准的?

The first, 首先,

element.style.WebkitFlex = 1;

is the preferred syntax. 是首选语法。 To reference vendor-prefixed CSS properties inside of JavaScript while using the non-normalized, exact CSS property name, we must rely on the use of bracket notation: 要在使用非规范化的精确CSS属性名称时引用 JavaScript内部的供应商前缀CSS属性 ,我们必须依赖括号表示法的使用:

element.style['-webkit-flex'] = 1;

This is, unfortunately, rather unwieldy. 不幸的是,这是相当笨拙的。 Dot notation affords itself better for these situations, and it is in that spirit why we also have the ability to reference vendor-prefixed properties using their camelCased counterparts. 点符号为这些情况提供了更好的表现,正是出于这种精神,我们还能够使用它们的camelCased对应物来引用供应商前缀属性。

In the process of normalizing property names between CSS and JavaScript, dashes are recognized as a delimiter. 在标准化CSS和JavaScript之间的属性名称的过程中,破折号被识别为分隔符。 They are, obviously, parsed out altogether in the resultant camelCased property name but, nevertheless, are taken into account. 显然,它们在生成的camelCased属性名称中完全被解析,但是,考虑到了它们。 In particular, the (delimiting) dashes signal where to apply different casing ( ie , the Uppercased lettering). 特别地,(划界)破折号表示应用不同套管的位置( ,大写字母)。 And, even though they are not alphabetical characters, a dash occupies the first position in (most) vendor-prefixed CSS properties, meaning the specific vendor's name prefix becomes capitalized. 并且,即使它们不是字母字符,短划线占据(大多数)以供应商为前缀的CSS属性中的第一个位置,这意味着特定供应商的名称前缀变为大写。 This procedure is also explained here . 此处还解释此过程。

Thus, element.style['-webkit-flex'] = 1; 因此, element.style['-webkit-flex'] = 1; becomes

element.style.WebkitFlex = 1;

Similar rules apply to the other vendors, such that we could also have... 类似的规则适用于其他供应商,因此我们也可以......

element.style.MozTransition = 'width 1s 0s ease-in-out';

element.style.OTransform = 'scaleX(1.5)';

As this answer explains, this normalization procedure is applied under the hood in certain libraries including jQuery , among others. 正如这个答案所解释的那样,这种规范化程序在某些库中应用,包括jQuery等。

Further evidence of this kind of procedure can be seen in other parts of the spec. 在规范的其他部分可以看到这种程序的进一步证据。 For example, the HTML5 dataset property defines similar rules for how to parse data-attributes. 例如, HTML5 dataset属性定义了如何解析数据属性的类似规则

As this is related with style property we should use established camel case notation that gets used with CSS properties. 由于这与style属性有关,我们应该使用与CSS属性一起使用的已建立的camel case表示法。

All style related properties are named like below: 所有与样式相关的属性都如下所示:

font-variant line-height letter-spacing word-spacing text-align font-variant line-height字母间距字间距text-align

So we should stay with established practice. 所以我们应该坚持既定的做法。

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

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