how to expand a text area when click on

后端 未结 9 1290
感情败类
感情败类 2021-02-02 10:17

Im working on a small project which has a textarea and i need help in making the text area expand on mouse click like that of twitter and facebook. the textarea should look lik

9条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 10:41

    Based in doog abides comment using jQuery, I enhanced the code to autoadjust approximately the number of rows acording to the length of the content, and return to 1 when focus is lost.

    HTML:

    
    

    jQuery:

    $('textarea.expand').focus(function () {
        $(this).animate({rows: Math.max(1,Math.ceil($(this).val().length/this.cols))}, 500);
    });
    $('textarea.expand').blur(function () {
        $(this).animate({rows: 1}, 500);
        //Resize back to one row if the textarea is manually resized via the handle
        $(this).css({height: ''});
        $(this).css({width: ''});
    });
    

提交回复
热议问题