javascript - 如何自动增长具有可变字体大小的文本框

标签 javascript jquery css

我正在编写一个网站,让您可以测试字体,就像谷歌字体一样,当用户输入更多文本时,您输入的文本区域应该会自动增长。

我试过 jaz303 的这个插件,如果字体大小保持不变,它工作正常。 https://github.com/jaz303/jquery-grab-bag/blob/master/javascripts/jquery.autogrow-textarea.js

  (function($) {
/**
 * Auto-growing textareas; technique ripped from Facebook
 *
 *
 * http://github.com/jaz303/jquery-grab-bag/tree/master/javascripts/jquery.autogrow-textarea.js
 */
$.fn.autogrow = function(options) {
  return this.filter('textarea').each(function() {
    var self = this;
    var $self = $(self);
    var minHeight = $self.height();
    var noFlickerPad = $self.hasClass('autogrow-short') ? 0 : parseInt($self.css('lineHeight')) || 0;
    var settings = $.extend({
      preGrowCallback: null,
      postGrowCallback: null
    }, options);

    var shadow = $('<div></div>').css({
      position: 'absolute',
      top: -10000,
      left: -10000,
      width: $self.width(),
      fontSize: $self.css('fontSize'),
      fontFamily: $self.css('fontFamily'),
      fontWeight: $self.css('fontWeight'),
      lineHeight: $self.css('lineHeight'),
      resize: 'none',
      'word-wrap': 'break-word'
    }).appendTo(document.body);

    var update = function(event) {
      var times = function(string, number) {
        for (var i = 0, r = ''; i < number; i++) r += string;
        return r;
      };

      var val = self.value.replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/\n$/, '<br/>&#xa0;')
        .replace(/\n/g, '<br/>')
        .replace(/ {2,}/g, function(space) {
          return times('&#xa0;', space.length - 1) + ' '
        });

      // Did enter get pressed?  Resize in this keydown event so that the flicker doesn't occur.
      if (event && event.data && event.data.event === 'keydown' && event.keyCode === 13) {
        val += '<br />';
      }

      shadow.css('width', $self.width());
      shadow.html(val + (noFlickerPad === 0 ? '...' : '')); // Append '...' to resize pre-emptively.

      var newHeight = Math.max(shadow.height() + noFlickerPad, minHeight);
      if (settings.preGrowCallback != null) {
        newHeight = settings.preGrowCallback($self, shadow, newHeight, minHeight);
      }

      $self.height(newHeight);

      if (settings.postGrowCallback != null) {
        settings.postGrowCallback($self);
      }
    }

    $self.change(update).keyup(update).keydown({
      event: 'keydown'
    }, update);
    $(window).resize(update);

    update();
  });
};

但是,我需要用户在测试时更改字体大小的可能性,并且由于某种原因,一旦我更改了大小,自动增长就不再起作用了。

这是我的测试 jsFiddle: http://jsfiddle.net/fquk6v3o/2/

最佳答案

解决方案是在 slider 值更改时重新启动 $("#autoGrowTextArea").autogrow();...

执行此操作的示例代码:

$("input[type='range']").change( function() { 
  $("#autoGrowTextArea").height("100px");
  $("#autoGrowTextArea").autogrow();
});

新的 JSfiddle 在这里:http://jsfiddle.net/newzy08/fquk6v3o/3/

关于javascript - 如何自动增长具有可变字体大小的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51262683/

相关文章:

math - 如何使 MediaWiki 中方程式的行高等于普通文本的行高(使其流畅)?

javascript - 使用我的下拉元素进行选择有什么问题

javascript - 关于AngularJS中数据绑定(bind)的疑问

javascript - Vue.js:根据方法对列表进行排序

javascript - 如何从拆分数组函数中乘以数据

javascript - Request-URI Too Large (Fusion table layers Google Maps)

jquery - Joomla - 带标签和下拉菜单的照片库

html - "Position: relative"没有top, right, left, bottom 是做什么用的?

javascript - 根据内容溢出动态插入分页符

javascript - 如何将脚本、图像和 css 文件保存到浏览器缓存中?