auto-resizing text input box html

后端 未结 3 1664

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:27

    You can try this.

    We put the content in a hidden span & then adjust width according to span scrollWidth.

    function Expander() {
    
        this.start = function () {
    
            $('#inputbox').on('keydown',function(e) {
    
                $("#hidden_span").html("");
                $("#hidden_span").append("

    "+$(this).val()+"

    "); var hidden_span_scroll_width=$("#hidden_span")[0].scrollWidth; if(hidden_span_scroll_width> 100||hidden_span_scroll_width< 600){ $(this).css("width",hidden_span_scroll_width); } }); } } $(function() { window.app = new Expander(); window.app.start(); });
    input {
        margin-left:3em;
        min-width:100px;
        max-width:600px;
    }
    
    #hidden_span{
        position:absolute;
        top:0px;
        left:0px;
        visibility:hidden;
        width:10px;
        white-space:nowrap;
        overflow:hidden;
    }
    
    
    
    
    
    
    

    
    
    
    
    
    Test page
    
    
    
    
    
    

提交回复
热议问题