Looks like I can do what I want using these styles:
.more-text {
line-height: 1.2em;
display:none;
white-space: nowrap;
}
.more-text-collapse {
display:inline-block;
max-width:400px;
height:1.2em;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}
.more-text-expand {
display:inline;
white-space: pre-wrap;
}
I give each of <divs>
just the more-text
class which lets them spread to their full width but hides them so they don't muck up the interface, then I compute their width and override class:
$('.more-text').each(function(i,el) {
var width = $(this).width();
$(this).addClass('more-text-collapse');
if(width > 400) {
$('<a>', {href:'#',class:'more-link'}).text('[more]').insertAfter(this);
}
});
Actually, we can take the width out of the CSS entirely so that we only have to define it in one place:
var maxWidth = 400;
$('.more-text').each(function(i,el) {
var width = $(this).width();
if(width > maxWidth) {
$(this).addClass('more-text-collapse').css('max-width',maxWidth);
$('<a>', {href:'#',class:'more-link'}).text('[more]').insertAfter(this);
} else {
$(this).addClass('more-text-expand');
}
});
Would be nice if I could animate the text expanding...