Angular Textarea 高度自动变化

很多前端开发的朋友可能都会遇到textarea 输入框的高度不能自动随着用户的输入变化的问题,今儿小生也遇到了, 并通过网络上的信息解决了这个问题,于是将解决方法贴上,以作备忘。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
directiveApp.directive('autoHeight', function(){
      function autoHeight(elem){
          elem.style.height = 'auto';
          elem.scrollTop = 0; //防抖动
          elem.style.height = elem.scrollHeight + 'px';
      }
 
      return {
          scope: {},
          link: function (scope, ele, attrs) {
              var oriEle = $(ele).get(0);
              $(oriEle).focus();
              $(oriEle).bind('keyup click', function(e) {
                  autoHeight($(this).get(0));
              });
              var timer = setInterval(function(){
                  if($(oriEle).val()) {
                      autoHeight(oriEle);
                      clearInterval(timer);
                  }
              }, 100);
          }
      };
  });<br>

 

1
2
Html code:
  <textarea auto-height></textarea>

  

不才,望谅。

 


posted @   RayChen1221  阅读(2124)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示