Chrome newtab里测量文字宽度的实现方法

2012-05-05 00:08:06 +08:00
 ehaagwlke
看Chrome的newtab页里,有一个测量文字宽度的实现方法,具体的代码在下面。
想请教的是,pxWidth里的那个measuringDiv.clientWidth * 1.15 里的1.15是怎么来的,谢谢!

/**
* Fills in an invisible div with the 'Most Visited' string so that
* its length may be measured and the nav dots sized accordingly.
*/
function measureNavDots() {
var measuringDiv = $('fontMeasuringDiv');
measuringDiv.textContent = localStrings.getString('mostvisited');
var pxWidth = Math.max(measuringDiv.clientWidth * 1.15, 80);

var styleElement = document.createElement('style');
styleElement.type = 'text/css';
// max-width is used because if we run out of space, the nav dots will be
// shrunk.
styleElement.textContent = '.dot { max-width: ' + pxWidth + 'px; }';
document.querySelector('head').appendChild(styleElement);
}
4414 次点击
所在节点    问与答
2 条回复
keakon
2012-05-05 17:33:42 +08:00
其实不是测量文字宽度。这个元素是position: absolute的,会正好包围文字,所以clientWidth就是文字宽度。那个1.15只是为了留点空白。
ehaagwlke
2012-05-06 15:19:06 +08:00
@keakon 从注释里看,应该是用来测量不同语言下“Most Vistied”这个字符串的宽度的。
那个1.15应该是为了让整个button的宽度有留白。 歇息!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/34683

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX