请教javascript 中ex px大小互换方法.

joe_smu 2006-08-17 10:42:44
请教javascript 中ex px互换方法.
因为通常得到的是px,但我需要把他转成ex,不知道有什么方法可以转?
...全文
181 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
leohuang 2006-08-17
  • 打赏
  • 举报
回复
ex是相关的计量单位,不是绝对的,相对于小写字母x的高度,没看明白怎么回事
这个东东不好算

joe_smu 2006-08-17
  • 打赏
  • 举报
回复
哎,看来只能在里面放一个 height:1ex width:1ex 的div,然后用现在的一些方法得到他的width的px了。看来很难有直接计算的转换函数了。
偶在这边的字符是 1ex=7px。所以leohuang兄的和字体有关系了。
leohuang 2006-08-17
  • 打赏
  • 举报
回复
修正一下
16ex=128px
放大一看才知道,1ex等于8px

<div style="font-size:16ex">Hello</div>
<div style="font-size:128px">Hello</div>
wuxinlangman 2006-08-17
  • 打赏
  • 举报
回复
呵呵!
leohuang 2006-08-17
  • 打赏
  • 举报
回复
测了一下1.6ex=14px

<div style="font-size:1.6ex">Hello</div>

<div style="font-size:14px">Hello</div>

其实ex这个单位是相对的,说是相对字母X的高度
look
---------------------------------------------------------------------------------
Font-size ex is somewhat similiar to the em measerument, as it uses the default size of the font (not the default size the browser uses, but the size the font is drawn at). Where the em uses the square in which the entire character resides, ex is the height of a lower case x.

You probably noticed that why you type something in Times New Roman, and then change font-family to Verdana, it seems to big, eventhough it takes up the same space on your screen.
What seems as a differance in size is actually a difference in legibility. Verdana has a bigger ex than Times New Roman does. This could be a useful measurement if you insist on using a strange font not everyone has, but you want it to be legible both in the intended font, and the back-up font.
wuxinlangman 2006-08-17
  • 打赏
  • 举报
回复
http://www.jonline.cn/shouce/css/css2/u_length.html
wuxinlangman 2006-08-17
  • 打赏
  • 举报
回复
相对长度单位。相对于字符“x”的高度。此高度通常为字体尺寸的一半。
如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸。
wuxinlangman 2006-08-17
  • 打赏
  • 举报
回复
1ex=1px/2

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧