auto-resizing text input box html

后端 未结 3 1721

I created an input (type text) box and made it auto-resize quite simply. However, there are a few glitches that I can\'t seem to fix:

  1. when I start typing the box s
3条回答
  •  半阙折子戏
    2021-01-26 19:48

    At first you can use jQuery methods to avoid crossbrowser problem. And scrollWidth returns either the width in pixels of the content of an element or the width of the element itself, whichever is greater. So enlarge newWidth only when content width strongly larger than element width.

    $("#inputbox").keydown(function(e) {
    
      var a = $(this).width(), newWidth;
    
    
      if(this.scrollWidth - a > 0){
        newWidth = a + 10;
      }else if(e.keyCode==8){
        newWidth = a - 10;
      }
      $(this).css("width", newWidth);
    
    });
    

    If you don't want to make input smaller when pressed backspace - delete else if part of code.

提交回复
热议问题