如何在隐藏溢出的范围内显示点("...")?

Pra*_*obh 150 html css

我的CSS:

#content_right_head span
{
  display:inline-block;
  width:180px;
  overflow:hidden !important;
}
Run Code Online (Sandbox Code Playgroud)

现在它正在显示内容内容

但我希望展示 内容内容......

我需要在内容后显示点.内容来自数据库动态.

san*_*eep 340

为此你可以使用text-overflow: ellipsis;财产.写这样的

span {
    display: inline-block;
    width: 180px;
    white-space: nowrap;
    overflow: hidden !important;
    text-overflow: ellipsis;
}
Run Code Online (Sandbox Code Playgroud)
<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book</span>
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

  • 我建议将'width'改为'max-width' (4认同)
  • 响应式布局怎么样?我的宽度不固定,也不是最大宽度. (4认同)
  • 这在IE11和firefox中不起作用。有什么好的解决方案吗? (2认同)

kir*_*nvj 18

如果您使用文本溢出:省略号,浏览器将在该容器中显示内容.但是,如果要指定点之前的字母数或删除一些内容并添加点,可以使用以下功能.

function add3Dots(string, limit)
{
  var dots = "...";
  if(string.length > limit)
  {
    // you can also use substr instead of substring
    string = string.substring(0,limit) + dots;
  }

    return string;
}
Run Code Online (Sandbox Code Playgroud)

打电话

add3Dots("Hello World",9);
Run Code Online (Sandbox Code Playgroud)

输出

Hello Wor...
Run Code Online (Sandbox Code Playgroud)

在这里看到它

function add3Dots(string, limit)
{
  var dots = "...";
  if(string.length > limit)
  {
    // you can also use substr instead of substring
    string = string.substring(0,limit) + dots;
  }

    return string;
}



console.log(add3Dots("Hello, how are you doing today?", 10));
Run Code Online (Sandbox Code Playgroud)


小智 15

text-overflow: ellipsis仅当显示 1 行时才起作用。如果更多,您可以使用display: -webkit-box属性以防您想显示更多 1 行。下面的代码为 2 行。

line-height: 1.6rem;
overflow: hidden;
display: -webkit-box; 
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
max-height: 3.2rem;
Run Code Online (Sandbox Code Playgroud)


And*_*ndo 13

我认为你正在寻找 text-overflow: ellipsis与...结合white-space: nowrap

在这里查看更多细节


Cha*_*mal 10

试试这个,

.truncate {
    display:inline-block;
    width:180px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
Run Code Online (Sandbox Code Playgroud)

.truncate类添加到元素中.


编辑,

以上解决方案无法在所有浏览器中使用.您可以尝试使用跨浏览器支持的jQuery插件.

(function ($) {
    $.fn.ellipsis = function () {
        return this.eachAsync({
            delay: 100,
            bulk: 0,
            loop: function (index) {
                var el = $(this);

                if (el.data("fullText") !== undefined) {
                    el.html(el.data("fullText"));
                } else {
                    el.data("fullText", el.html());
                }

                if (el.css("overflow") == "hidden") {
                    var text = el.html();
                    var t = $(this.cloneNode(true))
                                        .hide()
                                        .css('position', 'absolute')
                                        .css('overflow', 'visible')
                                        .width('auto')
                                        .height(el.height())
                                        ;

                    el.after(t);

                    function width() { return t.width() > el.width(); };

                    var func = width;
                    while (text.length > 0 && width()) {
                        text = text.substr(0, text.length - text.length * 25 / 100);
                        t.html(text + "...");
                    }

                    el.html(t.html());
                    t.remove();
                }
            }
        });
    };
})(jQuery);
Run Code Online (Sandbox Code Playgroud)

怎么打电话,

$("#content_right_head span").ellipsis();
Run Code Online (Sandbox Code Playgroud)


小智 5

好吧,“文本溢出:省略号”对我有用,但是如果我的限制基于“宽度”,我需要一个可以应用于行(在“高度”而不是“宽度”上)的解决方案,所以我做了这个脚本:

function listLimit (elm, line){
    var maxHeight = parseInt(elm.css('line-Height'))*line;

    while(elm.height() > maxHeight){
        var text = elm.text();
        elm.text(text.substring(0,text.length-10)).text(elm.text()+'...');
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,当我必须确保我的 h3 只有 2 行时,我会这样做:

$('h3').each(function(){
   listLimit ($(this), 2)
})
Run Code Online (Sandbox Code Playgroud)

我不知道这是否是性能需求的最佳实践,但对我有用。