With CSS, use “…” for overflowed block of multi-lines

后端 未结 16 2563
情话喂你
情话喂你 2020-11-22 07:15

with

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

\"...\" will be shown in the end of the line if overflowed. However, t

16条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 07:25

    I found a javascript trick, but you have to use the length of the string. Lets say you want 3 lines of width 250px, you can calculate the length per line i.e.

    //get the total character length.
    //Haha this might vary if you have a text with lots of "i" vs "w"
    var totalLength = (width / yourFontSize) * yourNumberOfLines
    
    //then ellipsify
    function shorten(text, totalLength) {
        var ret = text;
        if (ret.length > totalLength) {
            ret = ret.substr(0, totalLength-3) + "...";
        }
        return ret;
    }
    

提交回复
热议问题