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


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

文字溢出截斷2
1 2 3 4 5 6
|
<!DOCTYPE html><html><body> <style>@-webkit-keyframes width-change {0%,100%{width: 320px} 50%{width:260px}} |
設置外容器的font-size
、letter-spacing
、color
,並在子容器里恢復就可以單獨設置省略號。這里外容器設置font-size
的值等於2倍行高(余下要撐開的寬度可用letter-spacing
補足,也可僅用font-size
撐開全部的寬度),color:transparent
可以讓line-clamp既擠出文字又不截斷容器高度,外容器高度達到7行而不是默認表現的6行,從而達到需要的溢出截斷效果
實現詳解:
首先寫個省略號的容器<div className="ellipsis-container">

省略號容器的樣式要注意的一點是,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; |
接下來是正文的div(<div className="ellipsis-content">
), 它的高度就只文本的全高度:

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


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


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



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

