-
Notifications
You must be signed in to change notification settings - Fork 0
/
limitstringline.js
71 lines (63 loc) · 2.52 KB
/
limitstringline.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function($){
$.fn.extend({
limitStringLine: function(options) {
// options for the plugin
var defaults = {
lineNeeded: 2,
titleDiv: ".title"
};
var options = $.extend(defaults, options);
var lineNeeded = options.lineNeeded;
var parentDiv = this;
var titleDiv = options.titleDiv;
var calulateTotalLine = function(ref){
var divheight = jQuery(ref).find(titleDiv).height();
var lineheight = jQuery(ref).find(titleDiv).css('line-height');
if(typeof lineheight !== 'undefined'){
lineheight = lineheight.replace('px','');
}else{
lineheight = 20;
}
var totalLine = Math.round(divheight/parseInt(lineheight));
return totalLine;
};
var init = function(){
jQuery(parentDiv).each(function( index,ref ) {
var text = jQuery(ref).find(titleDiv).text();
jQuery(ref).append('<div class="fake--title" style="display:none;">'+text+'</div>');
});
resizeTitle();
jQuery( window ).resize(function() {
jQuery(parentDiv).each(function( index,ref ) {
jQuery(ref).find(titleDiv).text(jQuery(ref).find('.fake--title').text());
});
resizeTitle();
});
};
var resizeTitle = function(){
jQuery(parentDiv).each(function( index,ref ) {
var text = jQuery(ref).find(titleDiv).text();
var currentLine = calulateTotalLine(ref);
if(currentLine > lineNeeded){
if(typeof text !== 'undefined'){
var preText = "";
for(var i=0;i<text.length;i++){
var newText = text.substring(0, i);
newText = newText+'...' ;
jQuery(ref).find(titleDiv).text(newText);
var totalLine = calulateTotalLine(ref);
if(totalLine > lineNeeded){
jQuery(ref).find(titleDiv).text(preText);
break;
}else{
preText = newText;
}
}
}
}
});
};
init();
}
});
})(jQuery);