單行文本溢出
text-overflow屬性:clip,ellipsis,string
常用text-overflow:ellipsis省略號(hào)
.textOverflow{
width: 200px;
text-overflow: ellipsis;/*多余文本省略號(hào)代替*/
overflow: hidden;/*多余部分隱藏*/
white-space: nowrap;/*禁止換行*/
}
多行文本溢出
.textOverflow{
width: 200px;
overflow : hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;/*行數(shù)n*/
-webkit-box-orient: vertical;
}
還有其他方法
2018/01/08 更新
需要做到兼容(PC端Chrome板乙,F(xiàn)F,UC盯腌,Edge践剂,IE等)
.feedTxtAfter::after {
content: "...";
position: absolute;
bottom: 0;
right: 0;
padding-left: 15px;
padding-right: 3px;
padding-bottom: 3px;
background: -webkit-linear-gradient(left, transparent, #f8f8f8 55%);
background: -o-linear-gradient(right, transparent, #f8f8f8 55%);
background: -moz-linear-gradient(right, transparent, #f8f8f8 55%);
background: linear-gradient(to right, transparent, #f8f8f8 55%);
}
利用CSS的漸變屬性和偽類(lèi)after的content屬性鬼譬,與父元素進(jìn)行相對(duì)絕對(duì)定位,添加省略號(hào)
在JS中逊脯,用固定行數(shù)的內(nèi)容元素的高度來(lái)獲取是否超出行數(shù):
if ($('.feedTxt').length) {
var _feedTxt = $('.feedTxt');
for (var i = 0; i < _feedTxt.length; i++) {
if ($(_feedTxt[i]).height() == '72') {
$(_feedTxt[i]).addClass('feedTxtAfter');
}
}
}