不积跬步无以至千里----高度自适应的textarea

在某个项目里面,有这样的一个小需求。

textarea的高度自适应,当高度高于300px之后,textarea高度不再增高,出滚动条。当高度小于某个高度例如80px的时候,高度不再变小。

其实这个需求在很多地方都有出现过,例如微博的评论框,还有各种评论框。

谈不上什么有难度的技术,写下来当一个小插件积累。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>高度自适应的textarea</title>  
  6. <link rel='stylesheet' type='text/css' href='http://static.100.com/css/base.css?v=201404251401' />  
  7. <style>  
  8. .mod-solution{ padding:16px 21px 21px 21px; overflow:hidden;zoom:1; width:648px; margin:0 auto;}  
  9. .mod-solution .mhd h2{ font-size:24px; color:#535353; font-family:"Arial"; font-weight:normal;}  
  10. .so-count{ text-align:right; line-height:30px;}  
  11. .so-count em{ color:#ff8b50;}  
  12. .mod-solution .mbd{ overflow:hidden;zoom:1; padding-bottom:13px;}  
  13. .mod-solution .mft{ text-align:right;}  
  14. .mod-solution textarea,.copyCode{ width:626px; height:89px; overflow:hidden; border:1px solid #95c779; padding:5px 10px;}  
  15. .copyCode{ display:none; height:auto; min-height:89px;word-wrap: break-word;  
  16. word-break: normal;}  
  17. .mod-solution .greenBtn{ display:inline-block; padding: 0 14px; color:#fff; font-size:14px; height:32px; line-height:32px; background-color:#63c15f; text-decoration:none;}  
  18. .unPlBtn{  background-color: #eeeeee;  
  19.   color: #b4b4b4;  
  20.   display: inline-block;  
  21.   padding: 5px 15px;  
  22.   margin: 0;}  
  23.   .plBtn {  
  24.   display: none;  
  25.   background: #63c15f;  
  26.   color: #fff;  
  27.   padding: 5px 15px;  
  28. }  
  29.   
  30. </style>  
  31. </head>  
  32.   
  33. <body>  
  34. <div class="mod-solution">  
  35.     <div class="mhd">  
  36.         <h2>My Answer:</h2>  
  37.     </div>  
  38.     <div class="mbd">  
  39.         <p class="so-count">单词统计:<em>0</em></p>  
  40.         <div>  
  41.             <textarea></textarea>  
  42.             <div class="copyCode"></div>  
  43.         </div>  
  44.     </div>  
  45.     <div class="mft"><a href="javascript:void(0)" class="plBtn">提交答案</a><span class="unPlBtn">提交答案</span></div>  
  46. </div>  
  47. <script type="text/javascript" src="http://static.100.com/js/jquery/1.8.3/jquery.js?v=201404251401"></script>  
  48. <script type="text/javascript">  
  49. var cbox={  
  50.     init:function(){  
  51.         cbox.keyUpBind();  
  52.     },  
  53.     Bcount:function(str){ //计算单词数  
  54.         var i=0,j=0,c=0;   
  55.         var t=/[a-zA-Z]|([+-]?)\d*\.?\d+/;   
  56.         var bo=false;   
  57.         for(i=0,j=i+1;j<=str.length;i=j++){   
  58.             if(t.test(str.substring(i,j))&&!bo){   
  59.                 bo=true;c++;   
  60.             }else if(!t.test(str.substring(i,j))){   
  61.                 bo=false;   
  62.             }   
  63.         }   
  64.         return c;   
  65.     },  
  66.     keyUpBind:function(){ //输入框key事件的绑定  
  67.         $('.mod-solution textarea').live('keyup',function(){  
  68.             var num=cbox.Bcount($(this).val()),height=$(this).height();  
  69.             //if ($(this).val().match(/[\u4e00-\u9fa5]+/)) {  
  70.                 //alert('请不要不要输入中文哦,亲。');  
  71.                 //return;  
  72.             //}  
  73.             if(num>600){  
  74.                 alert('单词数不能超过600个。');  
  75.                 return;  
  76.             }  
  77.             if($(this).val()!=''){  
  78.                 $('.unPlBtn').hide();  
  79.                 $('.plBtn').css('display','inline-block');  
  80.             }else{  
  81.                 $('.unPlBtn').css('display','inline-block');  
  82.                 $('.plBtn').hide();  
  83.             }  
  84.             $('.so-count em').text(num);              
  85.         });  
  86.         $('.mod-solution textarea').live('focus',function(){  
  87.             if($(this).val()!=''){  
  88.                 $('.unPlBtn').hide();  
  89.                 $('.plBtn').css('display','inline-block');  
  90.             }  
  91.         })  
  92.         $('.mod-solution textarea').live('blur',function(){  
  93.             if($(this).val()==''){  
  94.                 $('.unPlBtn').css('display','inline-block');  
  95.                 $('.plBtn').hide();  
  96.             }  
  97.         });  
  98.         cbox.autoTextarea({  
  99.                 'maxHeight':300,  
  100.                 'obj':'textarea'  
  101.             });  
  102.     },  
  103.     /*文本域输入文字时  
  104.      *文字行数超过文本域高度的时候自动加高文本域高度  
  105.      *到达最大高度的时候出现滚动条  
  106.      *蛋疼的功能  
  107.      */  
  108.     autoTextarea:function(mySet){  
  109.         var defaults={  
  110.             minHeight:$(mySet.obj).height()-10   
  111.         };  
  112.         $(mySet.obj).bind("paste cut keydown keyup focus blur",function(){  
  113.               
  114.             var opts = $.extend({},defaults,mySet);  
  115.               
  116.             //$(this).height((defaults.minHeight) + 'px');  
  117.             var height,style=this.style;  
  118.             this.style.height =  opts.minHeight + 'px';  
  119.             if (this.scrollHeight > opts.minHeight) {  
  120.                 if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {  
  121.                     height = opts.maxHeight;  
  122.                     style.overflowY = 'scroll';  
  123.                 } else {  
  124.                     height = this.scrollHeight;  
  125.                     style.overflowY = 'hidden';  
  126.                 }  
  127.                 style.height = height  + 'px';  
  128.             }  
  129.         });  
  130.     }  
  131. }  
  132.   
  133. cbox.init();  
  134. </script>  
  135. </body>  
  136. </html>  


demo地址:http://jsbin.com/voqorecu/1/edit

ps:这个插件可以监听回车键。有空的时候可以copy代码去玩一下。

如果代码不行,请把jQuery.js的链接改成你本地的路径。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值