detect css text-overlow ellipsis with jquery

前端 未结 2 1900
日久生厌
日久生厌 2021-01-12 21:24

Here is a fiddle

CSS:

div{
    width:160px;
    text-overflow:ellipsis;
    overflow:hidden;
    white-space:nowrap;
    text-transform: uppercase;
}         


        
2条回答
  •  再見小時候
    2021-01-12 21:56

    There can be a difference between how the page looks in the browser and the calculations used to determine if the text will overflow. The box model doesn't always behave as we want. That said, you can compare the innerwidth with the scrollwidth.

    Here is a simple example that should correspond to the ellipse showing if true:

    if ($('#div')[0].scrollWidth >  $('#div').innerWidth()) {
        //ellipse should be showing because the text is in overflow
    }
    

    I updated your fiddle and it works as I expect. Your syntax for scrollwidth wasn't correct.

    e.scrollWidth vs. $(e)[0].scrollWidth
    

    Updated link to the new fiddle:

    http://jsfiddle.net/whipdancer/67upsqq8/1/

提交回复
热议问题