Skip to content

Instantly share code, notes, and snippets.

@parasharrajat
Forked from joshwnj/text_overflow.js
Created March 4, 2021 21:09
Show Gist options
  • Save parasharrajat/cfc0ce67d174507e58e03ebf1fac15be to your computer and use it in GitHub Desktop.
Save parasharrajat/cfc0ce67d174507e58e03ebf1fac15be 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>');
}
}
};
@parasharrajat
Copy link
Author

New Way

ScrollWidth - OffsetWidth > 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment