Workaround for issue with IE scrollWidth

后端 未结 3 1719
死守一世寂寞
死守一世寂寞 2021-02-07 08:44

I seem to have stumbled onto a bug in IE where the scrollWidth is off by 1px compared to the offsetWidth. The trigger to this seems to be dependent on the length of the text/ch

3条回答
  •  名媛妹妹
    2021-02-07 09:35

    Found a workable workaround for the issue that would work in ie9+. Requires checking the elements getBoundingClientRect() width in addition to the scroll and offset width.

    var boundingClientRectWidth = element.getBoundingClientRect().width;
    var iScrollWidth = div.scrollWidth;
    var iOffsetWidth = div.offsetWidth;
    
    var amIOverflowing = (iScrollWidth > iOffsetWidth) && (boundingClientRectWidth == iOffsetWidth);
    

    By check in IE if the boundingClient is forced to be the same size as the iOffsetWidth (instead of having a fractional width) we can ensure that we don't use the incorrect scroll width that is rounding up instead of down e.g. 273.36...

    See this jsfiddle: http://jsfiddle.net/gskfke6L/1/

提交回复
热议问题