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

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

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

  3. 支持长段不换行文字

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

110613_mncy_942601.png

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

转载于:https://my.oschina.net/youa/blog/383028

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值