textarea自适应高度

 1 (function ($) {
 2     $.fn.autoTextarea = function (options) {
 3         var defaults = {
 4             maxHeight: null,
 5             minHeight: $(this).height()
 6         };
 7         var opts = $.extend({}, defaults, options);
 8         return $(this).each(function () {
 9             $(this).bind("paste cut keydown keyup focus blur", function () {
10                 var height, style = this.style;
11                 this.style.height = opts.minHeight + 'px';
12                 if (this.scrollHeight > opts.minHeight) {
13                     if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {
14                         height = opts.maxHeight;
15                         style.overflowY = 'scroll';
16                     } else {
17                         height = this.scrollHeight;
18                         style.overflowY = 'hidden';
19                     }
20                     style.height = height + 'px';
21                 }
22             });
23         });
24     };
25 })(jQuery);
26 $("textarea#txt_show").autoTextarea({
27     maxHeight: 2000,
28     minHeight: 80
29 }); 

 

posted @ 2019-10-24 13:54  ——君——  阅读(204)  评论(0)    收藏  举报