Advertisement
koen_hendriks

jQuery Ellipses script

May 24th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.     $.fn.ellipsis = function()
  3.     {
  4.         return this.each(function()
  5.         {
  6.             var el = $(this);
  7.  
  8.             if(el.css("overflow") == "hidden")
  9.             {
  10.                 var text = el.html();
  11.                 var multiline = el.hasClass('multiline');
  12.  
  13.                 var t = $(this.cloneNode(true))
  14.                         .hide()
  15.                         .css('position', 'absolute')
  16.                         .css('overflow', 'visible')
  17.                         .width(multiline ? el.width() : 'auto')
  18.                         .height(multiline ? 'auto' : el.height())
  19.                     ;
  20.  
  21.                 el.after(t);
  22.  
  23.                 function height() { return t.height() > el.height(); };
  24.                 function width() { return t.width() > el.width(); };
  25.  
  26.                 var func = multiline ? height : width;
  27.  
  28.                 while (text.length > 0 && func())
  29.                 {
  30.                     text = text.substr(0, text.length - 1);
  31.                     t.html(text + "...");
  32.                 }
  33.  
  34.                 el.html(t.html());
  35.                 t.remove();
  36.             }
  37.         });
  38.     };
  39. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement