文本框根据输入内容自适应高度

jQuery实现

(function($){
  $.fn.autoTextarea = function(options) {
  var defaults={
  maxHeight:null,//文本框是否自动撑高,默认:null,不自动撑高;如果自动撑高必须输入数值,该值作为文本框自动撑高的最大高度
  minHeight:$(this).height() //默认最小高度,也就是文本框最初的高度,当内容高度小于这个高度的时候,文本以这个高度显示
  };
  var opts = $.extend({},defaults,options);
  return $(this).each(function() {
  $(this).bind("paste cut keydown keyup focus blur",function(){
  var height,style=this.style;
  this.style.height =  opts.minHeight + 'px';
  if (this.scrollHeight > opts.minHeight) {
  if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {
  height = opts.maxHeight;
  style.overflowY = 'scroll';
  } else {
  height = this.scrollHeight;
  style.overflowY = 'hidden';
  }
  style.height = height  + 'px';
  }
  });
  });
  };
  })(jQuery);

调用方式

$("#textarea_id").autoTextarea({maxHeight:220});  

js实现方式

      /**
       * 文本框根据输入内容自适应高度
      **/
      var autoTextarea = function (elem, extra, maxHeight) {
          extra = extra || 20;
          var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
              isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
              addEvent = function (type, callback) {
                  elem.addEventListener ?
                      elem.addEventListener(type, callback, false) :
                      elem.attachEvent('on' + type, callback);
              },
              getStyle = elem.currentStyle ? function (name) {
                  var val = elem.currentStyle[name];
                  if (name === 'height' && val.search(/px/i) !== 1) {
                      var rect = elem.getBoundingClientRect();
                      return rect.bottom - rect.top -
                          parseFloat(getStyle('paddingTop')) -
                          parseFloat(getStyle('paddingBottom')) + 'px';
                  };

                  return val;
              } : function (name) {
                      return getComputedStyle(elem, null)[name];
              },
              minHeight = parseFloat(getStyle('height'));
          elem.style.maxHeight = elem.style.resize = 'none';
          var change = function () {
              var scrollTop, height,
                  padding = 0,
                  style = elem.style;
              if (elem._length === elem.value.length) return;
              elem._length = elem.value.length;

              if (!isFirefox && !isOpera) {
                  padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'));
              };
              scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
              elem.style.height = minHeight + 'px';
              if (elem.scrollHeight > minHeight) {
                  if (maxHeight && elem.scrollHeight > maxHeight) {
                      height = maxHeight - padding;
                      style.overflowY = 'auto';
                  } else {
                      height = elem.scrollHeight - padding;
                      style.overflowY = 'hidden';
                  };

                  style.height = height + extra + 'px';
                  scrollTop += parseInt(style.height) - elem.currHeight;
                  document.body.scrollTop = scrollTop;
                  document.documentElement.scrollTop = scrollTop;
                  elem.currHeight = parseInt(style.height);
              };
          };
          addEvent('propertychange', change);
          addEvent('input', change);
          addEvent('focus', change);
          change();
      };

调用方式

autoTextarea(document.getElementById('textarea_id'));

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券