From 957ba15e6b62b0bcfc5991b398b6d0ad05d8bf35 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Fri, 18 Jun 2021 15:50:07 +0200 Subject: [PATCH] Replace scrollHeight/width with getBoundingClientRect --- textFit.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/textFit.js b/textFit.js index 0e32bd0..1bd0cf9 100644 --- a/textFit.js +++ b/textFit.js @@ -139,7 +139,7 @@ // Not guaranteed to always work if you use wonky line-heights var multiLine = settings.multiLine; if (settings.detectMultiLine && !multiLine && - innerSpan.scrollHeight >= parseInt(window.getComputedStyle(innerSpan)['font-size'], 10) * 2){ + innerSpan.getBoundingClientRect().height >= parseInt(window.getComputedStyle(innerSpan)['font-size'], 10) * 2){ multiLine = true; } @@ -156,7 +156,11 @@ while (low <= high) { mid = (high + low) >> 1; innerSpan.style.fontSize = mid + 'px'; - if(innerSpan.scrollWidth <= originalWidth && (settings.widthOnly || innerSpan.scrollHeight <= originalHeight)){ + var innerSpanBoundingClientRect = innerSpan.getBoundingClientRect(); + if ( + innerSpanBoundingClientRect.width <= originalWidth + && (settings.widthOnly || innerSpanBoundingClientRect.height <= originalHeight) + ) { size = mid; low = mid + 1; } else { @@ -187,7 +191,7 @@ // Calculate height without padding. function innerHeight(el){ var style = window.getComputedStyle(el, null); - return el.clientHeight - + return el.getBoundingClientRect().height - parseInt(style.getPropertyValue('padding-top'), 10) - parseInt(style.getPropertyValue('padding-bottom'), 10); } @@ -195,7 +199,7 @@ // Calculate width without padding. function innerWidth(el){ var style = window.getComputedStyle(el, null); - return el.clientWidth - + return el.getBoundingClientRect().width - parseInt(style.getPropertyValue('padding-left'), 10) - parseInt(style.getPropertyValue('padding-right'), 10); }