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

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

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <style type="text/css">
  7. .chackTextarea-area{line-height: 20px; font-size: 14px;padding: 0;border: 1px solid #CDCDCD; width: 400px;}
  8. </style>
  9. <script type="text/javascript" src="jquery-1.7.1.js"></script>
  10. <script type="text/javascript">
  11. (function($){
  12. $.fn.autoTextarea = function(options) {
  13. var defaults={
  14. maxHeight:null,
  15. minHeight:$(this).height()
  16. };
  17. var opts = $.extend({},defaults,options);
  18.  
  19. $(this).bind("paste cut keydown keyup focus blur",function(){
  20. var height,style=this.style;
  21. this.style.height = opts.minHeight + 'px';
  22. if (this.scrollHeight > opts.minHeight) {
  23. if (opts.maxHeight && this.scrollHeight > opts.maxHeight) {
  24. height = opts.maxHeight;
  25. style.overflowY = 'scroll';
  26. } else {
  27. height = this.scrollHeight;
  28. style.overflowY = 'hidden';
  29. }
  30. style.height = height + 'px';
  31. }
  32. });
  33. };
  34. })(jQuery);
  35. </script>
  36. </head>
  37. <body>
  38. <div>
  39. <textarea name="textarea" id="textarea" cols="60" rows="1" class="chackTextarea-area"></textarea>
  40. </div>
  41. <script type="text/javascript">
  42. $(".chackTextarea-area").autoTextarea({maxHeight:220});
  43. </script>
  44. </body>
  45. </html>
摘自:本文非原创,英雄沦落无人知?
如果您觉得好,可以打赏作者:
如果您觉得累了,是否想看点美女养养眼:猛戳>>朋友帮
如果您觉得皮了,是否想来点神吐槽:猛戳>>iPhone查询中

已有0条评论

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