-
Notifications
You must be signed in to change notification settings - Fork 1
/
多行标签展示收起.html
418 lines (398 loc) · 13.7 KB
/
多行标签展示收起.html
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>多个标签超出隐藏</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
padding: 0 16px;
}
.title {
text-align: center;
margin: 40px auto 0;
}
.list-con {
max-width: 375px;
height: 62px;
margin: 20px auto 0;
overflow: hidden;
/* border: 1px solid red;*/
display: flex;
flex-wrap: wrap;
}
.list-con-3,
.list-con-4 {
height: 62px;
}
.list-expand {
height: auto;
}
.label {
max-width: 132px;
max-width: calc(100% - 72px);
height: 24px;
border-radius: 22px;
background: #ecf2fe;
color: #0052d9;
font-size: 10px;
line-height: 24px;
text-align: center;
padding: 0 12px;
margin: 0 8px 8px 0;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.expand {
width: 50px;
font-size: 12px;
color: #666;
text-align: center;
margin: 5px auto;
/* cursor: pointer;*/
}
.expand-btn {
max-width: 100px;
/* cursor: pointer;*/
margin-right: 0;
}
</style>
</head>
<body>
<p class="title">样式一(按钮与容器分开):通过第一个标签偏移值判断</p>
<div class="list-con list-con-1">
<div class="label">人工智能</div>
<div class="label">人工智能与应用</div>
<div class="label">行业分析与市场数据</div>
<div class="label">标签标签标签标签标签标签标签标签</div>
<div class="label">标签</div>
<div class="label">啊啊啊</div>
<div class="label">宝宝贝贝</div>
<div class="label">微信</div>
<div class="label">吧啊啊</div>
<div class="label">哦哦哦哦哦哦哦哦</div>
</div>
<div class="expand expand-1">展开 ∨</div>
<script>
/*
方法一: 通过第一个标签左偏移值对比,如果有几个相同则说明有几个换行
*/
const listCon = document.querySelector(".list-con-1");
const expandBtn = document.querySelector(".expand-1");
console.log(listCon.children);
// HTMLCollection对象 item()、namedItem()方法 length属性
let firstLabelOffsetLeft = 0; // 第一个标签左侧偏移
let line = 1; // 记录行
const len = listCon.children.length;
for (let i = 0; i < len; i++) {
const _offsetLeft = listCon.children.item(i).offsetLeft;
if (i === 0) {
firstLabelOffsetLeft = _offsetLeft;
} else if (firstLabelOffsetLeft === _offsetLeft) {
line++;
console.log(line + "行");
}
}
// 如果大于一行则隐藏
if (line > 2) {
expandBtn.style = "display: show";
} else {
expandBtn.style = "display: none";
}
expandBtn.addEventListener("click", () => {
const _classList = listCon.classList;
if (_classList.contains("list-expand")) {
expandBtn.innerHTML = "展开 ∨";
} else {
expandBtn.innerHTML = "收起 ∧";
}
_classList.toggle("list-expand"); // 这个更简洁
});
</script>
<p class="title">样式一(按钮与容器分开): 通过高度判断</p>
<div class="list-con list-con-2">
<div class="label">人工智能</div>
<div class="label">人工智能与应用</div>
<div class="label">行业分析与市场数据</div>
<div class="label">标签标签标签标签标签标签标签标签</div>
<div class="label">标签</div>
<div class="label">啊啊啊</div>
<div class="label">宝宝贝贝</div>
<div class="label">微信</div>
<div class="label">吧啊啊</div>
<div class="label">哦哦哦哦哦哦哦哦</div>
</div>
<div class="expand expand-2">展开 ∨</div>
<script>
console.log("-----------样式1:高度比较--------------");
/*
方法二: 通过计算标签上top距离是否大于容器高
这种方法有个条件 就是必须要设定容器高度
*/
const listCon2 = document.querySelector(".list-con-2");
const expandBtn2 = document.querySelector(".expand-2");
console.log(listCon2.children);
const listCon2Height = listCon2.getBoundingClientRect().bottom;
console.log(listCon2Height);
const len2 = listCon2.children.length;
for (let i = 0; i < len2; i++) {
// console.log(listCon2.children.item(i).offsetWidth)
const _top = listCon2.children.item(i).getBoundingClientRect().top;
// 通过宽度判断如果所有标签大于容器则隐藏
if (_top >= listCon2Height) {
expandBtn2.style = "display: show";
break;
} else {
expandBtn2.style = "display: none";
}
}
expandBtn2.addEventListener("click", () => {
const _classList = listCon2.classList;
// console.log(_classList)
if (_classList.contains("list-expand")) {
expandBtn2.innerHTML = "展开 ∨";
} else {
expandBtn2.innerHTML = "收起 ∧";
}
_classList.toggle("list-expand");
});
</script>
<p class="title">样式二: 展开隐藏按钮和标签同级 - 通过高度判断</p>
<div id="app3">
<div class="list-con list-con-3" :class="{'list-expand': isExpand}">
<div class="label" v-for="item in labelArr.slice(0, labelLength)">
{{ item }}
</div>
<div
class="label expand-btn"
v-if="showExpandBtn"
@click="changeExpand"
>
{{ !isExpand ? '展开 ▼' : '隐藏 ▲' }}
</div>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
console.log("------------样式2:高度判断---------------");
/*
方法一: 通过计算所有标记宽度与容器宽比较,如果超出容器宽的n倍则表示标签大于n行需隐藏???
似乎行不通 如果剩余空白较大就无法计算了
方法二: 通过容器底部与标签top比较,如果有top值大于容器顶部bottom则表示超出容器隐藏
*/
const { createApp, nextTick } = Vue;
createApp({
props: {
maxLine: {
type: Number,
default: 2,
},
},
data() {
return {
labelArr: [],
isExpand: false,
showExpandBtn: false,
labelLength: 0,
hideLength: 0,
};
},
mounted() {
const labels = [
"人工智能",
"人工智能与应用",
"行业分析与市场数据",
"标签标签标签标签标签标签标签",
"标签A",
"啊啊啊",
"宝宝贝贝",
"微信",
"吧啊啊",
"哦哦哦哦哦哦哦哦",
"人工智能",
"人工智能与应用",
];
this.labelArr = labels;
this.labelLength = labels.length;
nextTick(() => {
this.init();
});
},
methods: {
init() {
const listCon = document.querySelector(".list-con-3");
const labels = listCon.querySelectorAll(".label:not(.expand-btn)");
const expandBtn = listCon.querySelector(".expand-btn");
let labelIndex = 0; // 渲染到第几个
const listConBottom = listCon.getBoundingClientRect().bottom; // 容器底部距视口顶部距离
for (let i = 0; i < labels.length; i++) {
const _top = labels[i].getBoundingClientRect().top;
if (_top >= listConBottom) {
// 如果有标签顶部距离超过容器底部则表示超出容器隐藏
this.showExpandBtn = true;
console.log("第几个索引标签停止", i);
labelIndex = i;
// this.hideLength = i
break;
} else {
this.showExpandBtn = false;
}
}
if (!this.showExpandBtn) {
return;
}
nextTick(() => {
const listConRect = listCon.getBoundingClientRect();
const expandBtn = listCon.querySelector(".expand-btn");
const expandBtnWidth = expandBtn.getBoundingClientRect().width;
const labelMaringRight = parseInt(
window.getComputedStyle(labels[0]).marginRight
);
for (let i = labelIndex - 1; i >= 0; i--) {
const labelRight =
labels[i].getBoundingClientRect().right - listConRect.left;
if (
labelRight + labelMaringRight + expandBtnWidth <=
listConRect.width
) {
this.hideLength = i + 1;
this.labelLength = this.hideLength;
break;
}
}
});
},
changeExpand() {
this.isExpand = !this.isExpand;
console.log(this.labelLength);
if (this.isExpand) {
this.labelLength = this.labelArr.length;
} else {
this.labelLength = this.hideLength;
}
},
},
}).mount("#app3");
</script>
<p class="title">样式二: 展开隐藏按钮和标签同级 - 通过计算行数判断</p>
<div id="app4">
<div class="list-con list-con-4" :class="{'list-expand': isExpand}">
<div class="label" v-for="item in labelList.slice(0, labelLength)">
{{ item }}
</div>
<div
class="label expand-btn"
v-if="showExpandBtn"
@click="changeExpand"
>
{{ !isExpand ? '展开 ▼' : '隐藏 ▲' }}
</div>
</div>
</div>
<!-- <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> -->
<script>
console.log("-------------样式2:通过行判断--------------");
/*
方法二: 通过所有标签与第一个标签左偏移值比较,有几个相同的则表示有几行
情况1: 标签宽度如果不限制, 则会出现标签和按钮大于容器的情况,所以要限制宽度
*/
// const { createApp, nextTick } = Vue
createApp({
data() {
return {
labelList: [],
isExpand: false,
showExpandBtn: false,
labelLength: 0,
hideLength: 0,
};
},
mounted() {
const labels = [
"人工智能",
"人工智能与应用",
"行业分析与市场数据报告行业分析与市场数据报告",
"标签标签标签标签标签标签标签",
"标签A",
"啊啊啊",
"宝宝贝贝",
"微信",
"吧啊啊",
"哦哦哦哦哦哦哦哦",
"人工智能",
"人工智能与应用",
];
this.labelList = labels;
this.labelLength = labels.length;
nextTick(() => {
this.init();
});
},
methods: {
init() {
const listCon = document.querySelector(".list-con-4");
const labels = listCon.querySelectorAll(".label:not(.expand-btn)");
const firstLabelOffsetLeft = labels[0].getBoundingClientRect().left; // 第一个标签左侧偏移量
const labelMaringRight = parseInt(
window.getComputedStyle(labels[0]).marginRight
);
let line = 0; // 几行
let labelIndex = 0; // 渲染第几个
for (let i = 0; i < labels.length; i++) {
const _offsetLeft = labels[i].getBoundingClientRect().left;
if (firstLabelOffsetLeft === _offsetLeft) {
line += 1;
}
console.log(line, i);
if (line > 2) {
this.showExpandBtn = true;
labelIndex = i;
// this.labelLength = this.hideLength
break;
} else {
this.showExpandBtn = false;
}
}
if (!this.showExpandBtn) {
return;
}
nextTick(() => {
const listConRect = listCon.getBoundingClientRect();
const expandBtn = listCon.querySelector(".expand-btn");
console.log(listConRect, expandBtn.getBoundingClientRect());
const expandBtnWidth = expandBtn.getBoundingClientRect().width;
for (let i = labelIndex - 1; i >= 0; i--) {
console.log(labels[i]);
const labelRight =
labels[i].getBoundingClientRect().right - listConRect.left;
console.log(labelRight, expandBtnWidth, labelMaringRight);
if (
labelRight + labelMaringRight + expandBtnWidth <=
listConRect.width
) {
this.hideLength = i + 1;
this.labelLength = this.hideLength;
break;
}
}
});
},
changeExpand() {
this.isExpand = !this.isExpand;
if (this.isExpand) {
this.labelLength = this.labelList.length;
} else {
this.labelLength = this.hideLength;
}
},
},
}).mount("#app4");
</script>
</body>
</html>