多行文本溢出省略號解決方案

1.JS計算高度

沒啥缺點,就是得引JQ,綜合來講算是最優解

<script>
	$(".research_item_text").each(function (i) {
	var divH = $(this).height();//外層元素定高度
	var $a = $("a", $(this)).eq(0);//內層a自適應高度
	while ($a.outerHeight() > divH) {
	$a.text($a.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "..."));
	};
	});
	</script>

2.-webkit-line-clamp

最大的缺點就是隻支持webkit,不過貴在不需要寫js

   text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 4;//行數
    line-clamp: 4;
    -webkit-box-orient: vertical;

2.::after

同樣不需要寫js,就是省略號比整個段落都長一塊兒,看起來是非常的不友好了

	p {
position:relative;
line-height:1.4em;
height:4.2em;
overflow:hidden;
}
7. p::after {
content:"...";
font-weight:bold;
 position:absolute;
bottom:0;
right:0;
padding:0 20px 1px 45px;
 background:url(http://newimg88.b0.upaiyun.com/newimg88/2014/09/ellipsis_bg.png) repeat-y;
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章