diff --git a/jquery.autosize.js b/jquery.autosize.js index 3fe9ce1..5d3a77d 100644 --- a/jquery.autosize.js +++ b/jquery.autosize.js @@ -94,6 +94,26 @@ resize: ($ta.css('resize') === 'none' || $ta.css('resize') === 'vertical') ? 'none' : 'horizontal' }); + // The mirror width must exactly match the textarea width, so using getBoundingClientRect because it doesn't round the sub-pixel value. + function setWidth() { + var style, width; + + if ('getComputedStyle' in window) { + style = window.getComputedStyle(ta); + width = ta.getBoundingClientRect().width; + + $.each(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'], function(i,val){ + width -= parseInt(style[val],10); + }); + + mirror.style.width = width + 'px'; + } + else { + // window.getComputedStyle, getBoundingClientRect returning a width are unsupported and unneeded in IE8 and lower. + mirror.style.width = Math.max($ta.width(), 0) + 'px'; + } + } + function initMirror() { var styles = {}; @@ -111,21 +131,7 @@ }); $(mirror).css(styles); - // window.getComputedStyle, getBoundingClientRect returning a width are unsupported/unneeded in IE8 and lower. - // The mirror width must exactly match the textarea width, so using getBoundingClientRect because it doesn't round the sub-pixel value. - if ('getComputedStyle' in window) { - var style = window.getComputedStyle(ta); - var width = ta.getBoundingClientRect().width; - - $.each(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'], function(i,val){ - width -= parseInt(style[val],10); - }); - - mirror.style.width = width + 'px'; - } - else { - mirror.style.width = Math.max($ta.width(), 0) + 'px'; - } + setWidth(); // This code block fixes a Chrome-specific issue: // The textarea overflow is probably now hidden, but Chrome doesn't reflow the text to account for the @@ -145,6 +151,8 @@ if (mirrored !== ta) { initMirror(); + } else { + setWidth(); } mirror.value = ta.value + options.append; diff --git a/jquery.autosize.min.js b/jquery.autosize.min.js index 0446e6d..dc68468 100644 --- a/jquery.autosize.min.js +++ b/jquery.autosize.min.js @@ -4,4 +4,4 @@ (c) 2013 Jack Moore - http://www.jacklmoore.com/autosize license: http://www.opensource.org/licenses/mit-license.php */ -(function(e){"function"==typeof define&&define.amd?define(["jquery"],e):e(window.jQuery||window.$)})(function(e){var t,o={className:"autosizejs",append:"",callback:!1,resizeDelay:10},i='