1分鐘實現多行文本框[textarea]自動高度jquery 插件

  1. 支持初始化加載textarea高度css

  2. 支持複製文字自動高度html

  3. 支持長段不換行文字dom

  4. 支持連續回車和連續刪除自動高度
    this

<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)
prototype

相關文章
相關標籤/搜索