mlellipsis.js
5.62 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
window.lineHeightDatabase={};
var $=require("jquery");
exports.init=function(){
/*获取一段文本共计多少行*/
$.fn.getTextLineNumber=function(keyword){
//省略后加上title
var $this=$(this);
var fontSize ;
if(!window.lineHeightDatabase[keyword]){
window.lineHeightDatabase[keyword]={fontSize:0,lineHeight:0};
}
if(window.lineHeightDatabase[keyword].fontSize!=0)
{
fontSize=window.lineHeightDatabase[keyword].fontSize;
}
else
{
fontSize= getFinalStyle(this[0],"fontSize");
window.lineHeightDatabase[keyword].fontSize=fontSize;
}
//获取计算后的样式
if(window.lineHeightDatabase[keyword].lineHeight!=0)
{
lineHeight=window.lineHeightDatabase[keyword].lineHeight;
window.lineHeightDatabase[keyword].lineHeight=lineHeight;
}
else
{
if(/msie/i.test(navigator.userAgent)){
var lineHeight = getFinalStyle(this[0],"lineHeight",fontSize);
}
else{
var lineHeight = getFinalStyle(this[0],"lineHeight");
}
}
if(this[0]==null)
return;
var height = this[0].clientHeight;
if(lineHeight == "norm"){
lineHeight = Number(fontSize*1.5);
var nowStyle = $this.attr("style")||"";
this[0].setAttribute("style",nowStyle+";line-height:"+lineHeight+"px");
}
else{lineHeight = Number(lineHeight);}
return parseInt(height/lineHeight);
}
$.fn.mlellipsis=function(row,keyword){
//省略后加上title
var $this=$(this);
var title = $this.attr("_title");
if(!title){
$this.attr("_title",$this.text());
}
else{
$this.text(title);
}
//获取计算后的样式
var fontSize ;
if(!window.lineHeightDatabase[keyword]){
window.lineHeightDatabase[keyword]={fontSize:0,lineHeight:0};
}
if(window.lineHeightDatabase[keyword].fontSize!=0)
{
fontSize=window.lineHeightDatabase[keyword].fontSize;
}
else
{
var fontSize= getFinalStyle(this[0],"fontSize");
window.lineHeightDatabase[keyword].fontSize=fontSize;
}
//获取计算后的样式
if(window.lineHeightDatabase[keyword].lineHeight!=0)
{
lineHeight=window.lineHeightDatabase[keyword].lineHeight;
}
else
{
if(/msie/i.test(navigator.userAgent)){
var lineHeight = getFinalStyle(this[0],"lineHeight",fontSize);
}
else{
var lineHeight = getFinalStyle(this[0],"lineHeight");
//window.lineHeightDatabase[keyword].lineHeight=lineHeight;
}
}
if(!this[0])
return;
var height = this[0].clientHeight;
if(lineHeight == "norm"){
lineHeight = Number(fontSize*1.5);
var nowStyle = $this.attr("style")||"";
this[0].setAttribute("style",nowStyle+";line-height:"+lineHeight+"px");
}
else{lineHeight = Number(lineHeight);}
//若高度足够,则不用省略
var dheight = Math.floor(row*lineHeight);
if(height >= dheight)
{
var str = this.text();
while(dheight*3<this[0].clientHeight){
$this.text(str.substring(0,str.length/2));
str = $this.text();
}
//减去末尾文字
while(dheight<this[0].clientHeight){
str = $this.text();
$this.text(str.replace(/(\s)*([a-zA-Z0-9]?|\W)(\.\.\.)?$/,"..."));
}
return row;
}
else
{
return parseInt(height/lineHeight);
}
}
$.fn.fillText=function(keyword){
//省略后加上title
var $this=$(this);
var title = $this.attr("_title");
if(!title){
$this.attr("_title",$this.text());
}
else{
$this.text(title);
}
//获取计算后的样式
var fontSize ;
if(!window.lineHeightDatabase[keyword]){
window.lineHeightDatabase[keyword]={fontSize:0,lineHeight:0};
}
if(window.lineHeightDatabase[keyword].fontSize!=0)
{
fontSize=window.lineHeightDatabase[keyword].fontSize;
}
else
{
var fontSize= getFinalStyle(this[0],"fontSize");
window.lineHeightDatabase[keyword].fontSize=fontSize;
}
//获取计算后的样式
if(window.lineHeightDatabase[keyword].lineHeight!=0)
{
lineHeight=window.lineHeightDatabase[keyword].lineHeight;
}
else
{
if(/msie/i.test(navigator.userAgent)){
var lineHeight = getFinalStyle(this[0],"lineHeight",fontSize);
}
else{
var lineHeight = getFinalStyle(this[0],"lineHeight");
//window.lineHeightDatabase[keyword].lineHeight=lineHeight;
}
}
if(!this[0])
return;
var height = this[0].clientHeight;
if(lineHeight == "norm"){
lineHeight = Number(fontSize*1.5);
var nowStyle = $this.attr("style")||"";
this[0].setAttribute("style",nowStyle+";line-height:"+lineHeight+"px");
}
else{lineHeight = Number(lineHeight);}
//若高度足够,则不用省略
if(height>5*lineHeight)
{
row=5;
}
else
{
row=(Math.floor(height/lineHeight)-1);
}
var titleHeight=$this.parent().find(".a-title").height()/30;
var subTitleHeight=$this.parent().find(".a-subtitle").height()/30;
if(titleHeight+subTitleHeight>=4)
{
row=4;
}
var dheight = Math.floor(row*lineHeight);
if(height >= dheight)
{
var str = this.text();
while(dheight*3<this[0].clientHeight){
$this.text(str.substring(0,str.length/2));
str = $this.text();
}
//减去末尾文字
while(dheight<this[0].clientHeight){
str = $this.text();
$this.text(str.replace(/(\s)*([a-zA-Z0-9]?|\W)(\.\.\.)?$/,"..."));
}
return row;
}
}
}
function getFinalStyle( obj, property,fontSize)
{
var s;
if(obj!=null&&obj.currentStyle){
s = parseInt(obj.currentStyle[property].replace("px",""));
}
else
if(window.getComputedStyle&&obj!=null){
s = window.getComputedStyle(obj, null)[property].replace("px","");
}
//兼容IEbug:IE解析getComputedStyle或currentStyle,然而若line-height=1.5,它会获取计算后是1.5,而其他浏览器获得1.5*line-height
/*if(fontSize!=undefined){
s = s*fontSize ;
}*/
return s;
}