多行文本溢出顯示省略號(…)全攻略

原文地址:http://www.css88.com/archives/5206

大家應該都知道用text-overflow:ellipsis屬性來實現單行文本的溢出顯示省略號(…)。當然部分瀏覽器還需要加寬度width屬性。

  1. overflow: hidden;
  2. text-overflow: ellipsis;
  3. white-space: nowrap;

但是這個屬性並不支持多行文本溢出顯示省略號,這裏根據應用場景介紹幾個方法來實現這樣的效果。

WebKit瀏覽器或移動端的頁面

在WebKit瀏覽器或移動端(絕大部分是WebKit內核的瀏覽器)的頁面實現比較簡單,可以直接使用WebKit的CSS擴展屬性(WebKit是私有屬性)-webkit-line-clamp ;注意:這是一個 不規範的屬性(unsupported WebKit property),它沒有出現在 CSS 規範草案中。

-webkit-line-clamp用來限制在一個塊元素顯示的文本的行數。 爲了實現該效果,它需要組合其他的WebKit屬性。
常見結合屬性:

  1. display: -webkit-box; 必須結合的屬性 ,將對象作爲彈性伸縮盒子模型顯示 。
  2. -webkit-box-orient 必須結合的屬性 ,設置或檢索伸縮盒對象的子元素的排列方式 。
  3. text-overflow: ellipsis;,可以用來多行文本的情況下,用省略號“…”隱藏超出範圍的文本 。
  1. overflow : hidden;
  2. text-overflow: ellipsis;
  3. display: -webkit-box;
  4. -webkit-line-clamp: 2;
  5. -webkit-box-orient: vertical;

這個屬性比較合適WebKit瀏覽器或移動端(絕大部分是WebKit內核的)瀏覽器。

具體例子可以查看http://www.css88.com/webkit/-webkit-line-clamp/

跨瀏覽器兼容的方案

比較靠譜簡單的做法就是設置相對定位的容器高度,用包含省略號(…)的元素模擬實現;

例如:

  1. p {
  2. position:relative;
  3. line-height:1.4em;
  4. /* 3 times the line-height to show 3 lines */
  5. height:4.2em;
  6. overflow:hidden;
  7. }
  8. p::after {
  9. content:"...";
  10. font-weight:bold;
  11. position:absolute;
  12. bottom:0;
  13. right:0;
  14. padding:0 20px 1px 45px;
  15. background:url(http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png) repeat-y;
  16. }

看demo:
src="http://jsfiddle.net/feiwen8772/qaxh927w/1/embedded/result,css,html,js" width="100%" height="300" frameborder="0" allowfullscreen="allowfullscreen" style="box-sizing: border-box; border-width: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; max-width: 100%;">

這裏注意幾點:

  1. height高度真好是line-height的3倍;
  2. 結束的省略好用了半透明的png做了減淡的效果,或者設置背景顏色;
  3. IE6-7不顯示content內容,所以要兼容IE6-7可以是在內容中加入一個標籤,比如用<span class="line-clamp">...</span>去模擬;
  4. 要支持IE8,需要將::after替換成:after

JavaScript 方案

用js也可以根據上面的思路去模擬,實現也很簡單,推薦幾個做類似工作的成熟小工具:

1.Clamp.js

下載及文檔地址:https://github.com/josephschmitt/Clamp.js
使用也非常簡單:

  1. var module = document.getElementById("clamp-this-module");
  2. $clamp(module, {clamp: 3});

DEMO:

id="cp_embed_AckqK" src="http://codepen.io/feiwen8772/embed/AckqK?height=468&theme-id=0&slug-hash=AckqK&default-tab=result&user=feiwen8772" scrolling="no" frameborder="0" height="468" allowtransparency="true" allowfullscreen="true" class="cp_embed_iframe undefined" style="box-sizing: border-box; border-width: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; max-width: 100%; width: 728px; overflow: hidden;">

2.jQuery插件-jQuery.dotdotdot

這個使用起來也很方便:

  1. $(document).ready(function() {
  2. $("#wrapper").dotdotdot({
  3. // configuration goes here
  4. });
  5. });

下載及詳細文檔地址:https://github.com/BeSite/jQuery.dotdotdothttp://dotdotdot.frebsite.nl/

參考:
http://www.cssmojo.com/line-clamp_for_non_webkit-based_browsers/#what-can-we-do-across-browsers
http://css-tricks.com/line-clampin/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章