Skip to content

Instantly share code, notes, and snippets.

@joshwnj
Created October 18, 2010 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshwnj/633274 to your computer and use it in GitHub Desktop.
Save joshwnj/633274 to your computer and use it in GitHub Desktop.
detect text overflow and crop, inserting an ellipsis
var TextOverflow = {
crop: function(elm) {
var w = elm.width(),
t = elm.text(),
child;
elm.html('<span style="overflow: hidden; white-space: nowrap">'+t+'</span>');
child = elm.children(":first-child");
while (t.length > 0 && child.width() > w) {
t = t.substr(0, t.length - 1);
child.html(t + '<span class="ellipsis">&hellip;</span>');
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment