-
-
Save parasharrajat/cfc0ce67d174507e58e03ebf1fac15be to your computer and use it in GitHub Desktop.
detect text overflow and crop, inserting an ellipsis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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">…</span>'); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New Way
ScrollWidth - OffsetWidth > 0