-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-textoverflow.js
45 lines (38 loc) · 1.47 KB
/
jquery-textoverflow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var fitText = function(container) {
var height = container.height();
var origText = $.trim(container.text());
var text = origText.split(' ');
var joinText = function() { return text.join(' ') + '...'; }
var sensor = container.clone().empty().css({ display: 'block',
visibility: 'hidden',
position: 'absolute',
width: container.width() + 'px',
height: 'auto',
'max-height': '1000px',
left: 0,
top: 0 });
container.after(sensor);
sensor.text(origText);
while (sensor.height() > height) {
text.pop();
sensor.text(joinText());
};
var ret = sensor.text();
sensor.remove();
if (ret != origText) { container.attr('title', origText); }
return ret;
};
$.fn.textOverflow = function(options) {
options = $.extend({ multiline: false }, options || {});
$(this).each(function() {
if (options.multiline) {
this.text(fitText(this));
} else {
this.
css({ 'white-space': 'nowrap',
'overflow': 'hidden',
'text-overflow': 'ellipsis' }).
attr('title', this.text());
}
});
};