fontSize之外属性用em做css单位

[2011-12-29 19:12:34 --@css-xhtml]

em很少用在fontSize以外的属性上,今天看jQuery源码发现了一行变动:
commit
-:style.left = ret || 0;
+:style.left = camelCase === "fontSize" ? "1em" : (ret || 0);


才注意到em这个单位用在例如left、margin、width等属性上的含义:
即相对于元素自身的字体大小来定位,这里不同于fontSize,仅fontSize是相对于父级元素的字体大小。

例如width:5em相当于宽度为5个自身元素的字体大小。

jQuery的为了得到以像素为单位的CSS值,用Dean提起的sstyle.left与pixLeft结合的方法去得到px为单位的值,当在currentStyle得到的值为5em时,如果设置style.left为5em,这样得来的left值为字体大小的5倍,是实际上要的结果的5倍。

所以对于BUG 706的解决方法很简单:
style.left = camelCase === "fontSize" ? "1em" : (ret || 0);



关于em单位,来自官方说明如下:http://www.w3.org/Style/Examples/007/units:

The em is simply the font size. In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion. Declarations such as 'text-indent: 1.5em' and 'margin: 1em' are extremely common in CSS.

本文禁止复制/转载!

共 1 评论

1. 2011-12-29 19:12:11,from 晓晓

大牛,又发表新做了!支持~~~



新增评论

分类导航