Skip to content

Instantly share code, notes, and snippets.

@extstopcodepls
Created March 18, 2014 07:57
Show Gist options
  • Save extstopcodepls/9615452 to your computer and use it in GitHub Desktop.
Save extstopcodepls/9615452 to your computer and use it in GitHub Desktop.
ellipsis function, works for ie
(function ($) {
$.fn.ellipsis = function () {
return this.each(function () {
var el = $(this);
if (!el.hasClass('running-ellipsis')) {
var text = el.html();
var divToMark = text.substr(text.indexOf('<div')); //div z trójkątem przy rozwijaniu/zwijaniu pozycji w lewym menu
if (divToMark.length > 1) {
text = text.substr(0, text.indexOf('<div')); //usunięcie tego trójąta ze sprawdzango tekstu
}
else {
divToMark = ''; //czyszczę divToMark, bo jeśli nie znajdzie '<div' to zwraca ostatni znak ze stringa
}
var elWidth = 115; //szerokość dla górnego menu
if (el.hasClass('widthToLMenuSubChap')) {
elWidth = 200; // szerokośc dla rodziałów w lewym menu
}
else if (el.hasClass('widthToLMenuChap')) {
elWidth = 160; // szerokośc dla podrodziałów w lewym menu
}
var textArr = text.split(/\s+/);
text = '';
var valLength = 0;
$.each(textArr, function (index, value) {
valLength = textWidth(value);
if (valLength > elWidth && elWidth > 0) {
while (valLength > elWidth) {
value = value.substr(0, value.length - 1);
valLength = textWidth(value);
}
value = value + '...';
}
text = text + ' ' + value;
});
el.html(text + divToMark);
el.addClass('running-ellipsis');
}
});
};
})(jQuery);
function textWidth(text) {
var html = $('<span>' + text + '</span>');
$('body').append(html);
var width = $('body').find('span:last').width();
$('body').find('span:last').remove();
return width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment