textarea自适应高度

时间:2019-10-24
本文章向大家介绍textarea自适应高度,主要包括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 }); 

原文地址:https://www.cnblogs.com/xujunbao/p/11731779.html