Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

jquery.autosize.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Autosize 1.13 - jQuery plugin for textareas
  2. // (c) 2012 Jack Moore - jacklmoore.com
  3. // license: www.opensource.org/licenses/mit-license.php
  4. (function ($) {
  5. var
  6. hidden = 'hidden',
  7. borderBox = 'border-box',
  8. lineHeight = 'lineHeight',
  9. copy = '<textarea tabindex="-1" style="position:absolute; top:-9999px; left:-9999px; right:auto; bottom:auto; -moz-box-sizing:content-box; -webkit-box-sizing:content-box; box-sizing:content-box; word-wrap:break-word; height:0 !important; min-height:0 !important; overflow:hidden;"/>',
  10. // line-height is omitted because IE7/IE8 doesn't return the correct value.
  11. copyStyle = [
  12. 'fontFamily',
  13. 'fontSize',
  14. 'fontWeight',
  15. 'fontStyle',
  16. 'letterSpacing',
  17. 'textTransform',
  18. 'wordSpacing',
  19. 'textIndent'
  20. ],
  21. oninput = 'oninput',
  22. onpropertychange = 'onpropertychange',
  23. test = $(copy)[0];
  24. // For testing support in old FireFox
  25. test.setAttribute(oninput, "return");
  26. if ($.isFunction(test[oninput]) || onpropertychange in test) {
  27. // test that line-height can be accurately copied to avoid
  28. // incorrect value reporting in old IE and old Opera
  29. $(test).css(lineHeight, '99px');
  30. if ($(test).css(lineHeight) === '99px') {
  31. copyStyle.push(lineHeight);
  32. }
  33. $.fn.autosize = function (options) {
  34. options = options || {};
  35. return this.each(function () {
  36. var
  37. ta = this,
  38. $ta = $(ta),
  39. mirror,
  40. minHeight = $ta.height(),
  41. maxHeight = parseInt($ta.css('maxHeight'), 10),
  42. active,
  43. i = copyStyle.length,
  44. resize,
  45. boxOffset = 0,
  46. value = ta.value,
  47. callback = $.isFunction(options.callback);
  48. if ($ta.css('box-sizing') === borderBox || $ta.css('-moz-box-sizing') === borderBox || $ta.css('-webkit-box-sizing') === borderBox){
  49. boxOffset = $ta.outerHeight() - $ta.height();
  50. }
  51. if ($ta.data('mirror') || $ta.data('ismirror')) {
  52. // if autosize has already been applied, exit.
  53. // if autosize is being applied to a mirror element, exit.
  54. return;
  55. } else {
  56. mirror = $(copy).data('ismirror', true).addClass(options.className || 'autosizejs')[0];
  57. resize = $ta.css('resize') === 'none' ? 'none' : 'horizontal';
  58. $ta.data('mirror', $(mirror)).css({
  59. overflow: hidden,
  60. overflowY: hidden,
  61. wordWrap: 'break-word',
  62. resize: resize
  63. });
  64. }
  65. // Opera returns '-1px' when max-height is set to 'none'.
  66. maxHeight = maxHeight && maxHeight > 0 ? maxHeight : 9e4;
  67. // Using mainly bare JS in this function because it is going
  68. // to fire very often while typing, and needs to very efficient.
  69. function adjust() {
  70. var height, overflow, original;
  71. // the active flag keeps IE from tripping all over itself. Otherwise
  72. // actions in the adjust function will cause IE to call adjust again.
  73. if (!active) {
  74. active = true;
  75. mirror.value = ta.value;
  76. mirror.style.overflowY = ta.style.overflowY;
  77. original = parseInt(ta.style.height,10);
  78. // Update the width in case the original textarea width has changed
  79. mirror.style.width = $ta.css('width');
  80. // Needed for IE to reliably return the correct scrollHeight
  81. mirror.scrollTop = 0;
  82. // Set a very high value for scrollTop to be sure the
  83. // mirror is scrolled all the way to the bottom.
  84. mirror.scrollTop = 9e4;
  85. height = mirror.scrollTop;
  86. overflow = hidden;
  87. if (height > maxHeight) {
  88. height = maxHeight;
  89. overflow = 'scroll';
  90. } else if (height < minHeight) {
  91. height = minHeight;
  92. }
  93. height += boxOffset;
  94. ta.style.overflowY = overflow;
  95. if (original !== height) {
  96. ta.style.height = height + 'px';
  97. if (callback) {
  98. options.callback.call(ta);
  99. }
  100. }
  101. // This small timeout gives IE a chance to draw it's scrollbar
  102. // before adjust can be run again (prevents an infinite loop).
  103. setTimeout(function () {
  104. active = false;
  105. }, 1);
  106. }
  107. }
  108. // mirror is a duplicate textarea located off-screen that
  109. // is automatically updated to contain the same text as the
  110. // original textarea. mirror always has a height of 0.
  111. // This gives a cross-browser supported way getting the actual
  112. // height of the text, through the scrollTop property.
  113. while (i--) {
  114. mirror.style[copyStyle[i]] = $ta.css(copyStyle[i]);
  115. }
  116. $('body').append(mirror);
  117. if (onpropertychange in ta) {
  118. if (oninput in ta) {
  119. // Detects IE9. IE9 does not fire onpropertychange or oninput for deletions,
  120. // so binding to onkeyup to catch most of those occassions. There is no way that I
  121. // know of to detect something like 'cut' in IE9.
  122. ta[oninput] = ta.onkeyup = adjust;
  123. } else {
  124. // IE7 / IE8
  125. ta[onpropertychange] = adjust;
  126. }
  127. } else {
  128. // Modern Browsers
  129. ta[oninput] = adjust;
  130. }
  131. $(window).resize(adjust);
  132. // Allow for manual triggering if needed.
  133. $ta.bind('autosize', adjust);
  134. // The textarea overflow is now hidden. But Chrome doesn't reflow the text after the scrollbars are removed.
  135. // This is a hack to get Chrome to reflow it's text.
  136. ta.value = '';
  137. ta.value = value;
  138. // Call adjust in case the textarea already contains text.
  139. adjust();
  140. });
  141. };
  142. } else {
  143. // Makes no changes for older browsers (FireFox3- and Safari4-)
  144. $.fn.autosize = function (callback) {
  145. return this;
  146. };
  147. }
  148. }(jQuery));