React 多行省略的展開與收起


當字數多到一定程度就顯示省略號點點點。最初只是簡單的點點點,之后花樣越來越多,點點點加下箭頭,點點點加更多,點點點加更多加箭頭…。多行省略就是大段文字后面的花式點點點。

而我實現的是這樣的:

006tNc79gy1g249clv9nmj30fi07ywfu

006tNc79gy1g249d349i2j30fe0cudid

實現代碼:

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
import React, {Component} from 'react';

class Ellipsis extends Component {
constructor(props) {
super(props);
this.state = {
show: false,
};
}

toggleShow = () => {
this.setState((state) => ({
show: !state.show,
}));
}

render() {
const { show } = this.state;
return(
<div className={'ellipsis' + (show ? '' : ' show' )}>
<style jsx>{`
/*
* 行高 h
* 最大行數 n
* ...更多容器的寬 w
* 字號 f
*/

.ellipsis {
position: relative;
background: rgb(230, 230, 230);
width: 260px;
text-align: justify;
max-height: unset;
line-height: 18px; /* h */

}

.ellipsis.show {
max-height: 108px; /* h*n */
overflow: hidden;
}

.ellipsis-container {
position: relative;
display: ${show ? 'block' : '-webkit-box'};
-webkit-box-orient: vertical;
-webkit-line-clamp: 6; /* n */
font-size: 65px; /* w */
color: transparent;
}

.ellipsis-content {
color: #000;
display: inline;
vertical-align: top;
font-size: 12px; /* f */
}

.ellipsis-ghost {
position:absolute;
z-index: 1;
top: 0;
left: 50%;
width: 100%;
height: 100%;
color: #000;
}

.ellipsis-ghost:before {
content: "";
display: block;
float: right;
width: 50%;
height: 100%;
}

.ellipsis-placeholder {
content: "";
display: block;
float: right;
width: 50%;
height: 108px; /* h*n */
}

.toggle-button {
float: right;
font-size: 12px; /* f */
width: 50px; /* w */
height: 18px; /* h */
margin-top: -18px; /* -h */
opacity: 0.5;

}

.toggle-button.showbutton {
position: absolute;
bottom: -18px;
left: 50%;
margin-left: -50px;
}
`}</style>
<div className="ellipsis-container">
<div className="ellipsis-content">AICoin,全球數字資產行情數據應用領跑者,致力於更高效地 提供有價值的信息,提升資產管理的效率。AICoin ,擁有全面而優質的數字資產資源,自 2013 年以 來,深耕細作,為用戶提供實時行情、專業K線、指標分析、資訊動態等專業的數據服務。AICoin,全球 數字資產行情數據應用領跑者,致力於更高效地提供有價值的信息,提升資產管理的效率。AICoin ,擁 有全面而優質的數字資產資源,自 2013 年以來,深耕細作,為用戶提供實時行情、專業K線、指標分 析、資訊動態等專業的數據服務。
</div>
<div className="ellipsis-ghost">
<div className="ellipsis-placeholder"></div>
<div className={'toggle-button' + (show ? ' showbutton' : '')}>
<span style={{ color: `${show ? 'transparent' : '#000000'}`,
paddingRight: '5px',
}}
>...
</span>
<span style={{ cursor: 'pointer', color: 'blue' }}
onClick={this.toggleShow}
>
{show ? '[收起]' : '[展開]'}
</span>
</div>
</div>
</div>
</div>
);
}
}

export default Ellipsis;

原理詳解:

文字溢出截斷

文字溢出截斷

文字溢出截斷

 

1
2
3
4
5
6
<!DOCTYPE html><html><body>
<style>@-webkit-keyframes width-change {0%,100%{width: 320px} 50%{width:260px}}/*測試*/</style>
<div style="font-size: 12px;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 6;color: red;line-height: 18px;position: relative;-webkit-animation: width-change 8s ease infinite;background: rgb(230, 230, 230);">
<div style="color:#000;display: inline;vertical-align: top;background: rgb(204, 204, 204);">騰訊成立於1998年11月,是目前中國領先的互聯網增值服務提供商之一。成立10多年來,騰訊一直秉承“一切以用戶價值為依歸”的經營理念,為億級海量用戶提供穩定優質的各類服務,始終處於穩健發展狀態。2004年6月16日,騰訊控股有限公司在香港聯交所主板公開上市(股票代號700)。</div>
</div>
</body></html>

-webkit-line-clamp是webkit內核的私有css屬性,用於進行多行省略,在安卓和ios上全支持。但它固定使用省略號,無法直接擴展。而且自帶了溢出截斷邏輯,作用於容器高度。仔細考察可發現它使用的省略號是單字符,可以用文字css屬性如font-size,letter-spacing,color等控制。

文字溢出截斷2

文字溢出截斷2

 

1
2
3
4
5
6
<!DOCTYPE html><html><body>
<style>@-webkit-keyframes width-change {0%,100%{width: 320px} 50%{width:260px}}/*測試*/</style>
<div style="font-size: 36px;letter-spacing: 28px;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 6;color: red;line-height: 18px;position: relative;-webkit-animation: width-change 8s ease infinite;background: rgb(230, 230, 230);">
<div style="color:#000;display: inline;font-size: 12px;vertical-align: top;letter-spacing: 0;background: rgb(204, 204, 204);">騰訊成立於1998年11月,是目前中國領先的互聯網增值服務提供商之一。成立10多年來,騰訊一直秉承“一切以用戶價值為依歸”的經營理念,為億級海量用戶提供穩定優質的各類服務,始終處於穩健發展狀態。2004年6月16日,騰訊控股有限公司在香港聯交所主板公開上市(股票代號700)。</div>
</div>
</body></html>

設置外容器的font-sizeletter-spacingcolor,並在子容器里恢復就可以單獨設置省略號。這里外容器設置font-size的值等於2倍行高(余下要撐開的寬度可用letter-spacing補足,也可僅用font-size撐開全部的寬度),color:transparent可以讓line-clamp既擠出文字又不截斷容器高度,外容器高度達到7行而不是默認表現的6行,從而達到需要的溢出截斷效果

實現詳解:

首先寫個省略號的容器<div className="ellipsis-container">

image-20190416110711099

 

省略號容器的樣式要注意的一點是,font-size的值要足夠大,以至於有足夠空間讓你放下"…[展開]", 而"…[展開]"只遮住了省略號的空間, 這樣就不會出現遮住半個字的現象(此現象會有點丑):

1
2
3
4
5
6
7
8
.ellipsis-container {
position: relative;
display: ${show ? 'block' : '-webkit-box'};
-webkit-box-orient: vertical;
-webkit-line-clamp: 6; /* n */
font-size: 65px; /* w */
color: transparent;
}

接下來是正文的div(<div className="ellipsis-content">), 它的高度就只文本的全高度:

image-20190416111720814

 

然后是設置"…[展開]“和”[收起]"的div:

image-20190416112009101

 

image-20190416112039123

先把.ellipsis-ghost右移到.ellipsis-container的一半位置, 然后.ellipsis-ghost::before的高度要與.ellipsis-ghost的高度保持一致, 而.ellipsis-ghost的高度是與它的父級元素(.ellipsis-container)的高度保持一致的, 又因.ellipsis-container的高度是被.ellipsis-content的文本撐開的高度, 因此, .ellipsis-ghost::before的高度其實是與文本的高度保持一致的

image-20190416112111232

image-20190416112154542

 

接着把.ellipsis-placeholder的高度設置為最小文本的呈現高度(.ellipsismax-height), 如果文本高度大於max-height, 由於右浮動的原理, 且.ellipsis-ghost:before的高度大於.ellipsis-placeholder的高度, 則可以把"…[展開]"顯示出來, 如上顯示;

如果文本高度小於或等於max-height, 則 .ellipsis-ghost:before的高度小於或等於.ellipsis-placeholder的高度, 那么"…[展開]"則會飄走而不顯示, 如下:

image-20190416115537471

 

image-20190416115552162

 

image-20190416115610657

 

最后可以在.toggle-button里設置一個點擊事件來控制是否顯示全文:

image-20190416142344967

 

image-20190416142512303

 

 

文章就分享到這,歡迎關注“前端大神之路” 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM