Know what overflow:hidden has hidden

后端 未结 4 1151
长情又很酷
长情又很酷 2020-12-13 16:31

I want to know if there is any way you can call and use what the overflow:hidden has well hidden.

To clarify what I mean, in this example I would like t

4条回答
  •  旧时难觅i
    2020-12-13 16:45

    This can give an estimate of the hidden text in the particular case in which the text in the div can wrap.

     
    This is shown. This is hidden

    add to the .text css class:

     line-height: 20px;
    

    At runtime, you can use the .height() jquery function to get the computed height of the inner div. Dividing it by the line-height, you can get the number of lines to which the text has been wrapped, with only the first being shown. Then you can guess the last word being shown/not shown and substring your text there.

    var text = $("#inner").html();
    var height = $("#inner").height();
    var lineheight = $("div.text").height();
    var rows = Math.round(height / lineheight);
    alert("computed inner height: "
        + $("#inner").height()
        + " | rows: " + rows);
    alert("Hidden text: "
        + text.substring(
            text.indexOf(" ",Math.round(text.length / rows))));
    

提交回复
热议问题