Dynamically Scrolling a Textarea

后端 未结 5 773
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 08:42

I have a textarea html element on my page that gets reloaded via ajax. The whole textarea is returned each time not just its content, and the content grows over time. Along

相关标签:
5条回答
  • 2020-11-29 09:03

    Using jQuery, $("textarea").scrollHeight(99999) works great on Firefox and Chrome but not on IE. It appears to set it to the max number of lines in the textarea, whereas scrollHeight is supposed to be the number of pixels. (Awesome show great job IE). This appears to work though:

          $("textarea").scrollTop(99999)
          $("textarea").scrollTop($("textarea").scrollTop()*12)
    

    I think this assumes a 12px font-height. I would love to find a more robust/straightforward way to do this.

    0 讨论(0)
  • 2020-11-29 09:10

    Instead of using the timeout, call that function on every AJAX response - provided you can tweak it.

    That would free your browser from unnecessary timeouts.

    0 讨论(0)
  • 2020-11-29 09:14

    As a quick hack you can just do this:

    textArea.scrollTop = 99999;
    

    Another option is to try it in a timer:

    setTimeout(function()
    {
        var textArea = document.getElementById('outputTextResultsArea');
        textArea.scrollTop = textArea.scrollHeight;
    }, 10);
    
    0 讨论(0)
  • 2020-11-29 09:19

    For using like textarea in your example give your textarea a name like "id=LiveTextArea"

    Then add buttom of your javascript this (LiveTextArea is the id name):

    LiveTextArea.scrollTop = LiveTextArea.scrollHeight;

    So would look like:

    var textArea = document.getElementById('outputTextResultsArea');

    textArea.scrollTop = textArea.scrollHeight;

    Now the textarea will dynamicly scroll caret/down on any new entry.

    0 讨论(0)
  • 2020-11-29 09:21

    I ended up using this for Internet Explorer:

    textArea.createTextRange().scrollIntoView(false);
    

    and this for other browsers:

    textArea.scrollTop = textArea.scrollHeight;
    
    0 讨论(0)
提交回复
热议问题