js控制textarea高度自适应(摘)

phper| 阅读:1631 发表时间:2012-10-07 00:52:00 js
摘要:工作要用到textarea自适应高度的场景,于是就网上找了个,闲话少说,上代码:

工作要用到textarea自适应高度的场景,于是就网上找了个,闲话少说,上代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.chackTextarea-area{line-height: 20px; font-size: 14px;padding: 0;border: 1px solid #CDCDCD; width: 400px;}
</style>
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
(function($){
    $.fn.autoTextarea = function(options) {
        var defaults={
            maxHeight:null,
            minHeight:$(this).height()
        };
        var opts = $.extend({},defaults,options);

        $(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);
</script>
</head>
<body>
<div>
    <textarea name="textarea" id="textarea" cols="60" rows="1" class="chackTextarea-area"></textarea>
</div>   
<script type="text/javascript">
    $(".chackTextarea-area").autoTextarea({maxHeight:220});
</script>
</body>
</html>
摘自:本文非原创,英雄沦落无人知?
如果您觉得好,可以打赏作者:
如果您觉得累了,是否想看点美女养养眼:猛戳>>朋友帮
如果您觉得皮了,是否想来点神吐槽:猛戳>>iPhone查询中

已有0条评论

昵称:
邮箱:

  • 最新评论

iPhone查询中 - bbs.ipcxz.com 朋友帮 - www.pengyb.cn iPhone查询中 - bbs.ipcxz.com
反馈
微信订阅号