Cross-browser multi-line text overflow with ellipsis appended within a fixed width and height

前端 未结 25 2760
栀梦
栀梦 2020-11-22 16:19

I made an image for this question to make it easier to understand.

Is it possible to create an ellipsis on a

with a fixed width and multiple
25条回答
  •  孤城傲影
    2020-11-22 16:44

    With this code there is no need for an extra wrapper div if the element has it's height limited by a max-height style.

    // Shorten texts in overflowed paragraphs to emulate Operas text-overflow: -o-ellipsis-lastline
    $('.ellipsis-lastline').each(function(i, e) {
        var $e = $(e), original_content = $e.text();
        while (e.scrollHeight > e.clientHeight)
            $e.text($e.text().replace(/\W*\w+\W*$/, '…'));
        $e.attr('data-original-content', original_content);
    });
    

    Also it saves the original text in a data attribute that can be displayed using only styles, eg. on mouse over:

    .ellipsis-lastline {
        max-height: 5em;
    }
    .ellipsis-lastline:before {
        content: attr(data-original-content);
        position: absolute;
        display: none;
    }
    .ellipsis-lastline:hover:before {
        display: block;
    }
    

提交回复
热议问题