1分钟实现多行文本框[textarea]自动高度jquery 插件

  1. 支持初始化加载textarea高度

  2. 支持复制文字自动高度

  3. 支持长段不换行文字

  4. 支持连续回车和连续删除自动高度

<div class="form-group">
    <label class="col-sm-3 control-label no-padding-right" for="form-field-5"> 内容</label>
    <div class="col-sm-9">
        <textarea class="col-sm-8" id="form-field-5" placeholder="请输入内容..."></textarea>
    </div>
</div>
$.extend({
    textareaAutosize_dc:function(){
        var autoSizeFn=function(){}
        autoSizeFn.prototype={
            autosize:function(){
                var fontsize = $(this).css("font-size").replace("px","");//文字大小
                var fontrowcount = ($(this).width()/fontsize);//每行文字个数
                var textArray = $(this).val().split("\n");               
                var currentEnterCount=textArray.length;//获取行数
                $(textArray).each(function(){
                    //检查每行文字量是否超过行容量 如果超过 贼需要加行, 超几行加几行
                   if(this.length>fontrowcount){
                       currentEnterCount+=this.length/fontrowcount;
                   }
                });
                var lineHeight=Number($(this).css("line-height").replace("px",""));
                $(this).height( lineHeight*(currentEnterCount+1));
            },addEvent:function(){
                //注册事件监听
                var self=this;
                 $("textarea").on("keyup",function(e){
                        self.autosize.call(this);
                 });
            },initAllHeight:function(){
                //初始化所有高度
                var self=this;
                 $("textarea").each(function(){
                     self.autosize.call(this);
                 });
            }
            ,init:function(){
                this.addEvent();
                this.initAllHeight();
            }
        }
       new autoSizeFn().init();
    }
}).fn.extend({
    textareaAutosize_dc:function(){
       var domSelf = this;
       var autoSizeFn=function(domSelf){
           this.domSelf=domSelf;
       }
        autoSizeFn.prototype={
            autosize:function(){
                var fontsize = $(this).css("font-size").replace("px","");//文字大小
                var fontrowcount = ($(this).width()/fontsize);//每行文字个数
                var textArray = $(this).val().split("\n");               
                var currentEnterCount=textArray.length;//获取行数
                $(textArray).each(function(){
                    //检查每行文字量是否超过行容量 如果超过 贼需要加行, 超几行加几行
                   if(this.length>fontrowcount){
                       currentEnterCount+=this.length/fontrowcount;
                   }
                });
                var lineHeight=Number($(this).css("line-height").replace("px",""));
                $(this).height( lineHeight*(currentEnterCount+1));
            },addEvent:function(){
                //注册事件监听
                var self=this;
                 $(this.domSelf).on("keyup",function(e){
                        self.autosize.call(this);
                 });
            },initHeight:function(){
                var self=this;
                //初始化所有高度
                $(this.domSelf).each(function(){
                     self.autosize.call(this);
                 });
            }
            ,init:function(){
                this.addEvent();
                this.initHeight();
            }
        }
       new autoSizeFn(domSelf).init();
    }
});
//调用自动高度
//$.textareaAutosize_dc();//应用到所有textarea中
$('textarea').textareaAutosize_dc();//应用到指定的textarea中

原理:其实就是 文本框高度=行高*(换行数+1)

你可能感兴趣的:(jquery插件,textarea)