Maison > interface Web > tutoriel CSS > le corps du texte

多行文本溢出显示省略号(…)

PHPz
Libérer: 2017-03-12 17:13:58
original
1539 Les gens l'ont consulté

大家应该都知道用<a href="http://www.php.cn/wiki/868.html" target="_blank">text-</a><a href="http://www.php.cn/wiki/923.html" target="_blank">overflow</a>:ellipsis属性来实现单行文本的溢出显示省略号(…)。当然部分浏览器还需要加宽度<a href="http://www.php.cn/wiki/835.html" target="_blank">width</a>属性。

css 代码:
  1. overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    Copier après la connexion

但是这个属性并不支持多行文本溢出显示省略号,这里根据应用场景介绍几个方法来实现这样的效果。

WebKit浏览器或移动端的页面

在WebKit浏览器或移动端(绝大部分是WebKit内核的浏览器)的页面实现比较简单,可以直接使用WebKit的CSS扩展属性(WebKit是私有属性)-webkit-line-clamp ;注意:这是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中。

-webkit-line-clamp用来限制在一个块元素显示的文本的行数。 为了实现该效果,它需要组合其他的WebKit属性。
常见结合属性:

  1. <a href="http://www.php.cn/wiki/927.html" target="_blank">display</a>: -webkit-box; 必须结合的属性 ,将对象作为弹性伸缩盒子模型显示 。

  2. -webkit-box-orient 必须结合的属性 ,设置或检索伸缩盒对象的子元素的排列方式 。

  3. text-overflow: ellipsis;,可以用来多行文本的情况下,用省略号“…”隐藏超出范围的文本 。

css 代码:
  1. overflow : hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    Copier après la connexion

这个属性比较合适WebKit浏览器或移动端(绝大部分是WebKit内核的)浏览器。

具体例子可以查看http://www.php.cn/

跨浏览器兼容的方案

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

例如:

css 代码:
  1. p {
        position:relative;
        line-height:1.4em;
        /* 3 times the line-height to show 3 lines */
        height:4.2em;
        overflow:hidden;
    }
    p::after {
        content:"...";
        font-weight:bold;
        position:absolute;
        bottom:0;
        right:0;
        padding:0 20px 1px 45px;
        background:url(http://www.php.cn/) repeat-y;
    }
    Copier après la connexion

这里注意几点:

  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

下载及文档地址:http://www.php.cn/
使用也非常简单:

js 代码:
  1. var module = document.getElementById("clamp-this-module");
    $clamp(module, {clamp: 3});
    Copier après la connexion

2.jQuery插件-jQuery.dotdotdot

这个使用起来也很方便:

js 代码:
  1. $(document).ready(function() {
    $("#wrapper").dotdotdot({
    //configuration goes here
    });
    });
    Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!