forked from kuzeko/jquery-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.truncate.min.js
3 lines (3 loc) · 1.19 KB
/
jquery.truncate.min.js
1
2
3
(function(h){function j(c,a,d,b,e,f,h){var i,g;h?(i=d===0?"":a.slice(-d),g=a.slice(-b)):(i=a.slice(0,d),g=a.slice(0,b));if(e.html(g+f).width()<e.html(i+f).width())return b;g=parseInt((d+b)/2,10);i=h?a.slice(-g):a.slice(0,g);e.html(i+f);if(e.width()===c)return g;e.width()>c?b=g-1:d=g+1;return j(c,a,d,b,e,f,h)}h.fn.truncate=function(c){c=h.extend({width:"auto",token:"…",center:!1,addclass:!1,addtitle:!1},c);return this.each(function(){var a=h(this),d={fontFamily:a.css("fontFamily"),fontSize:a.css("fontSize"),
fontStyle:a.css("fontStyle"),fontWeight:a.css("fontWeight"),"font-variant":a.css("font-variant"),"text-indent":a.css("text-indent"),"text-transform":a.css("text-transform"),"letter-spacing":a.css("letter-spacing"),"word-spacing":a.css("word-spacing"),display:"none"},b=a.text();d=h("<span/>").css(d).html(b).appendTo("body");var e=d.width(),f=!isNaN(parseFloat(c.width))&&isFinite(c.width)?c.width:a.width();e>f&&(d.text(""),c.center?(f=parseInt(f/2,10)+1,e=b.slice(0,j(f,b,0,b.length,d,c.token,!1))+c.token+
b.slice(-1*j(f,b,0,b.length,d,"",!0))):e=b.slice(0,j(f,b,0,b.length,d,c.token,!1))+c.token,c.addclass&&a.addClass(c.addclass),c.addtitle&&a.attr("title",b),a.html(e));d.remove()})}})(jQuery);