在javascript中可以使用css3线夹吗?

时间:2012-08-08 00:43:53

标签: javascript css css3 webkit

目前为了垂直限制文本框,并添加省略号,只有一个使用line-clamp&的webkit css3解决方案。盒定向。

演示(safari或chrome):http://jsfiddle.net/ArKeu/136/

现在其他任何地方都不支持,甚至不使用前缀。所以我的问题是,是否可以在纯JavaScript中轻松完成此操作。简而言之,让一个句子在多行上流动,并在添加省略号后添加一对句子。

感谢您的帮助。

1 个答案:

答案 0 :(得分:7)

我将代码格式this answer修改为纯JS。您没有指定行数,而是指定框的高度。您可以通过将行数乘以line-height来轻松地根据行数计算高度。

<强> HTML

<div id="fos">
    <p>text here</p>
</div>​

<强>的JavaScript

var container = document.getElementById("fos");
var containerHeight = container.clientHeight;
var para = container.children[0];

while (para.clientHeight > containerHeight) {
    para.innerText = para.innerText.replace(/\W*\s(\S)*$/, '...');
}

DEMO