我在这里问过一个类似的问题:How to auto resize text in fixed DIV according to the text's length?
而且它起作用了。但它只在一行文本上工作。比方说,我希望我的文本换行到2行,如果它更多(溢出),那么去调整大小…
有没有办法做到这一点?
谢谢!
发布于 2010-11-10 20:26:23
如果你知道字体大小,字体样式和行高,我想如果是你的站点,你就知道了:)。您可以编写javascript函数,将字符添加到div,直到它的高度小于或等于两行的高度。
下面是一个可以在所有浏览器中进行单行检查的示例。说实话,这不是我的解决方案,但是我的同事允许我发布它:)。
function fitInWidth(name) {
this.empty().append(name);
while (this.height() > 19) {
name = name.substring(0, name.length - 3);
while (name.charAt(name.length - 1) == ' ') name = name.substring(0, name.length - 2);
this.empty().append(name = name + '…');
}
return name;
}发布于 2010-11-10 21:02:26
可以使div inline,然后找到高度,然后使它block,但使用新的高度。
$("#element").css("display","inline").css({height: $("#element").height(), display: "block"});https://stackoverflow.com/questions/4143303
复制相似问题