AngularJS directive input width resize by keyup

前端 未结 5 797
余生分开走
余生分开走 2021-01-06 06:36

I created a directive in order to make a input that has width automatically resized when keyup (like Google Contacts). However it seems not to be ok, because the width of ea

5条回答
  •  逝去的感伤
    2021-01-06 07:18

    I've done this before. The solution I used is having an off-screen SPAN with the same text in it, with the same exact font as your textbox, and interrogating its width.

    I might have something like this:

    
    
    .textbox-copy {
      position: absolute;
      left: -9999px;
      top: -9999px;
      font: -webkit-small-control;
      font: -moz-field;
      font-size: 13px;
    }
    

    Then on keydown set the innerHTML of that SPAN, and check its current width. Note that, in Chrome and Firefox at least, an unstyled textbox has a special font of its own. It doesn't just inherit Arial or whatever.

提交回复
热议问题