处理前段文本溢出,多余内容显示点点

处理前段文本溢出,多余内容显示点点

大家应该都知道用text-overflow:ellipsis属性来实现单行文本的溢出显示省略号(…)。当然部分浏览器还需要加宽度width属性。

css 代码:
  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;,可以用来多行文本的情况下,用省略号“…”隐藏超出范围的文本 。
 
   
css 代码:
  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/

-o-ellipsis-lastline

从 Opera 10.60 开始,text-overflow属性有了一个名为-o-ellipsis-lastline的值。应用后的效果就像名字一样,在文本的最后一行加上省略号。这个方法比楼上的方法简单多了,可惜也不在标准之内//(ㄒoㄒ)//

[css]  view plain  copy
  1. p <span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">{</span>  
  2.     <span style="font-family:Consolas, Courier, monospace !important;font-weight:bold;">overflow</span><span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">:</span> <span style="font-family:Consolas, Courier, monospace !important;color:#993333;">hidden</span><span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">;</span>  
  3.     <span style="font-family:Consolas, Courier, monospace !important;font-weight:bold;">white-space</span><span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">:</span> <span style="font-family:Consolas, Courier, monospace !important;color:#993333;">normal</span><span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">;</span>  
  4.     <span style="font-family:Consolas, Courier, monospace !important;font-weight:bold;">height</span><span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">:</span> <span style="font-family:Consolas, Courier, monospace !important;color:#993333;">3em</span><span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">;</span>  
  5.     text-overflow<span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">:</span> -o-ellipsis-lastline<span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">;</span>  
  6. <span style="font-family:Consolas, Courier, monospace !important;color:#00aa00;">}</span>  
  7. ​  

Demo: http://jsfiddle.net/Cople/ash5v/


跨浏览器兼容的方案

比较靠谱简单的做法就是设置相对定位的容器高度,用包含省略号(…)的元素模拟实现;

例如:

css 代码:
  1. {
  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:http://jsfiddle.net/feiwen8772/qaxh927w/1/?utm_source=website&utm_medium=embed&utm_campaign=qaxh927w

这里注意几点:

  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使用也非常简单:

 
   
js 代码:
  1. var module = document.getElementById("clamp-this-module");
  2. $clamp(module, {clamp: 3});
2.jQuery插件-jQuery.dotdotdot

这个使用起来也很方便:

 
   
js 代码:
  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/


下面的是js脚本加正则表达式来实现(通过从后向前逐个删除末尾字符,直至元素的高度小于父元素高度)

[javascript]  view plain  copy
  1. $<span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#3366cc;">".figcaption"</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span>.<span style="font-family:Consolas, Courier, monospace !important;color:#660066;">each</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#000066;font-weight:bold;">function</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span>i<span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">{</span>  
  2.     <span style="font-family:Consolas, Courier, monospace !important;color:#000066;font-weight:bold;">var</span> divH <span style="font-family:Consolas, Courier, monospace !important;color:#339933;">=</span> $<span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#000066;font-weight:bold;">this</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span>.<span style="font-family:Consolas, Courier, monospace !important;color:#660066;">height</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span><span style="font-family:Consolas, Courier, monospace !important;color:#339933;">;</span>  
  3.     <span style="font-family:Consolas, Courier, monospace !important;color:#000066;font-weight:bold;">var</span> $p <span style="font-family:Consolas, Courier, monospace !important;color:#339933;">=</span> $<span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#3366cc;">"p"</span><span style="font-family:Consolas, Courier, monospace !important;color:#339933;">,</span> $<span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#000066;font-weight:bold;">this</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span>.<span style="font-family:Consolas, Courier, monospace !important;color:#660066;">eq</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#cc0000;">0</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span><span style="font-family:Consolas, Courier, monospace !important;color:#339933;">;</span>  
  4.     while <span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span>$p.<span style="font-family:Consolas, Courier, monospace !important;color:#660066;">outerHeight</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span> <span style="font-family:Consolas, Courier, monospace !important;color:#339933;">></span> divH<span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span> <span style="font-family:Consolas, Courier, monospace !important;color:#009900;">{</span>  
  5.         $p.<span style="font-family:Consolas, Courier, monospace !important;color:#660066;">text</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span>$p.<span style="font-family:Consolas, Courier, monospace !important;color:#660066;">text</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span>.<span style="font-family:Consolas, Courier, monospace !important;color:#660066;">replace</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">(</span><span style="font-family:Consolas, Courier, monospace !important;color:#009966;font-style:italic;">/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/</span><span style="font-family:Consolas, Courier, monospace !important;color:#339933;">,</span> <span style="font-family:Consolas, Courier, monospace !important;color:#3366cc;">"..."</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span><span style="font-family:Consolas, Courier, monospace !important;color:#339933;">;</span>  
  6.     <span style="font-family:Consolas, Courier, monospace !important;color:#009900;">}</span><span style="font-family:Consolas, Courier, monospace !important;color:#339933;">;</span>  
  7. <span style="font-family:Consolas, Courier, monospace !important;color:#009900;">}</span><span style="font-family:Consolas, Courier, monospace !important;color:#009900;">)</span><span style="font-family:Consolas, Courier, monospace !important;color:#339933;">;</span>  

Demo: http://jsfiddle.net/Cople/DrML4/5/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值