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 > |
不才,望谅。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步