javascript改进textarea文本域自适应内容高度

场景需求:根据输入内容自适应高度,textarea不能满足需求,需要该进

autoTextarea.js:

define('autoTextarea',[],function(){

     return{

           /*

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

                *cfg{ //配置参数

                     elem:elem,//输入元素

                      extra:extra,// 设置光标与输入框保持的距离(默认0)

                      maxHeight:maxHeight,//设置最大高度(可选)

                      initEvent:initEvent, //是否初始化文本高度(true / false)

                      initScroll:initScroll,//是否初始化文本框滚回顶部(true/false)

                      changeHeight:changeHeight  // 回调函数

                  }

           */

          fn:function(cfg){

                if(!cfg){return}

               var                     elem=cfg.elem,extra=cfg.extra||0,maxHeight=cfg.maxHeight||0,initEvent=cfg.initEvent||false,initScroll=cfg.initScroll||false;    

               var addEvent=function(type,callback){

            elem.addEventListener?elem.addEventListener(type,callback,false):elem.attachEvent('on'+type,callback);

           };

           var getStyle;

       if(elem.currentStyle){

        getStyle=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;

        };

      }else{

         getStyle=function(name){

           return  getComputedStyle(elem,null)[name];

           };

        }

          var minHeight=parseFloat(getStyle('height'))>=0?parseFloat(getStyle('height')):0;

         elem.style=resize='none';//如果不希望使用这可以自由的伸展textarea的高度可以设置其他值

      var change=function(type){

          var  height;

        var style=elem.style;

       if(elem._length===elem.value.length)  return;

       elem._length=elem.value.length;

      var  scrollTop=document.body.scrollTop||document.documentElement.scrollTop;

      elem.style.height=minHeight+'px';

     if(elem.scrollHeight>=minHeight){

               if(maxHeight&&elem.scrollHeight>maxHeight){

                     height=maxHeight;

                    style.overflowY='auto';

               }else{

                     height=elem.scrollHeight;

                    style.overflowY='hidden';

              }

            style.height=height+extra+'px';

           if(initScroll){

                scrollTop+=parseInt(style.height)-elem.currHeight;

               document.body.scrollTop=scrollTop;

               document.documentElement.scrollTop=scrollTop;   

                }

            elem.currHeight=parseInt(style.height);

            if(cfg&&typeof cfg.changeHeight==='function'){cfg.changeHeight(height+extra)}

            if(type=='keydown'||type=='input'){elem.focus()}

        }

      } ;

      if(initEvent){change();}

      addEvent('focus',function(){change('focus')});

     addEvent('input',function(){change('input')}) ;   

          }

       }

});

 

调用如下:

<textarea  name='conten'  row='1' class='simulation-textarea  validate  ipt'  type='text'>

   输入内容区,高度回随内容的高度变化,不会出现滚动条

</textarea>

 引入autoTextarea.js

  require(['auto Textarea'],function(autoTextareaUtils)){

     var autoTextarea=autoTextareaUtils.fn;

    autoTextarea({elem:$('.simulation-textarea')[0],initEvent:false})

})

几乎兼容所有主流浏览器,IE可以兼容到IE8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值