jquery 如何在可见div高度内的最后一个单词上确定并应用“...”?

xfb7svmp  于 2022-11-29  发布在  jQuery
关注(0)|答案(2)|浏览(228)

我有一个overflow: hidden; div,它有很多文本和一个css内联定义的height
这意味着在给定的高度内可以看到X个单词。
是否可以确定给定style="height: 145px;" div高度内的最后一个单词,并将“...”应用于该单词?我将如何进行此操作?
fiddle说明
HTML示例(其中溢出应用于“Cubilia dolor...”之后)

<div class="cut-off" style="height: 145px;">
LOREM IPSUM
Lorem ipsum dolor sit amet, et sem sollicitudin diam nascetur tortor ipsum. Cubilia dolor mi nihil, amet et, amet in sit, aenean metus commodo Lorem ipsum 
</div>
0qx6xfy6

0qx6xfy61#

您可以使用text-overflow: ellipsis;来完成此操作。
就像

.cutoff{
   ...
  text-overflow: ellipsis;
}

更新

结果表明,这只适用于单行(不适用于换行的多行div)。
这里是另一个镜头(copied on the work from this fiddle,所以完全归功于作者,而不是我)
第一次

ni65a41a

ni65a41a2#

很抱歉,花了一段时间,但我终于得到了一个解决这个优秀的问题:******

function ellipsis(elem){
    var html=elem.html(), //get the html of the respective element
        text=html.split('\n'), //split the html by lines and store it into an array
        output=[], //an array to store the final output
        char, //an element to store each charachter to process 
        totalWidth=0, //an array to store the processed width of the text
        charCount=0; //an array to count how many charachters are allowed to be shown in each line
    $.each(text,function(i,v){ //iterating through the array of lines
        charCount=0; //emptying the processed variables
        totalWidth=0; //emptying the processed variables
        for(x=0;x<v.length;x++){ //iterating through each letter of the text
            char=v.substr(x,1); //getting the current charachter
            $('body').append('<span class="char" style="font-size='+elem.css('font-size')+';">'+char+'</span>'); //putting the current charachter inside a span and giving the span the font size of the element so that we can get the width of the charachter
            totalWidth+=$('.char').width(); //adding the width of the charachter to totalWidth to start comparing the total processed width with the element width
            $('.char').remove(); //removing the added span
            if(totalWidth<=elem.width()){ //checking if the total width of charachters surpassed the width of the element
                charCount++; //if not then allowing another charachter to be shown
            }
            else{
                x=v.length; //if it has surpassed it then exiting the for loop
            }
        }
        if(charCount!=v.length){ //checking if the number of allowed charachters is less than the actual line charachter's length
            charCount-=4; //if it is less, taking 3 charachters off (for three dots) and one more for converting the number into the index of the charachter
            output.push(v.substr(0,charCount)+'...'); //showing the allowed number of charachters and adding three dots to the end and storing it to the output array
        }
        else{
            output.push(v); //if it is not less than the actual text then add the actual text to the output
        }
    });
    $.each(output,function(i,v){ //iterating through the output values
        if(v=='')output.splice(i,1); //removing the empty line breaks

    });
    output=output.join('<br>'); //getting the text together and recreating the line breaks by putting a br tag between them
    elem.html(output); //replacing the new processed value with the old value
}
ellipsis($('.cut-off')); //calling the function on the desired element

解释在代码中,但随时问任何弹出的问题。
还要记住,这只是一个小函数,它不能涵盖项目中可能出现的所有情况,但只要对它稍加改进,它就可以完美地用于任何情况。(只有在调用此函数的元素只包含文本且未被br标记分割时,此函数才有效)

相关问题