vaakash / jquery-collapser

jQuery collapser is a small and useful jQuery plugin for collapsing/truncating an element text by words, characters and lines with a flexible API. It is an all in one plugin with multiple functionalities to truncate a paragraph or any element as desired.
MIT License
99 stars 35 forks source link

Line count wrong when switching to HTML #3

Open seanhak opened 9 years ago

seanhak commented 9 years ago

Hey, great job, very useful plugin.

I did some changes to your codebase to allow the truncated content to maintain html-tags. This is useful if you want to keep bold and italic text, or in my case paragraphs. I changed this by deleting this line of code: eWrap.html( eWrap.text() ); however now the lines are not calculated right, and often ends with decimals. Any ideas? My guess is that jQuery height calculation is a bit wonky when calculating padding etc.

I also added following code to hide button and cancel hiding:

if (s.remaining['lines'] < (s.o.truncate - 1)) {
 if ($.isFunction(s.o.controlBtn)) {
  s.o.controlBtn.call(s.e).hide();
 }                      
return false;
}

It would be nice if this was part of the core features.

Cheers

bhalial commented 9 years ago

Sup Seanhak, I was having the same issues you're describing. I solved em by using

var toReplace = s.remaining[s.mode].toFixed() + ( s.remaining[s.mode] == 1 ? ' ' + plural[s.mode][0] : ' ' + plural[s.mode][1] );

at line 334ish. So I added .toFixed() to the remaining s.mode.

This works pretty well so far.