Yes, you could use some plug-in, but this logic is not incredibly hard to write yourself. The basic notion is to keep reducing the amount of text until it fits. This sounds expensive, but in practice it works fine, and could be optimized if necessary.
// Populate an element with text so as to fit into height
function truncate(elt, content, height) {
function getHeight(elt) { return elt.getBoundingClientRect().height; }
function shorten(str) { return str.slice(0, -1); }
elt.style.height = "auto";
elt.textContent = content;
// Shorten the string until it fits vertically.
while (getHeight(elt) > height && content) {
elt.textContent = (content = shorten(content)) + '...';
}
}
To use this:
truncate(div, "Some very long text to be truncated", 200)
To use with HTML content, adapt/extend as necessary.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…