Scrolling Overflowed DIVs with JavaScript

前端 未结 6 543
逝去的感伤
逝去的感伤 2020-12-02 22:26

I\'ve got a div that uses overflow:auto to keep the contents inside the div as it is resized and dragged around the page. I\'m using some ajax to retrieve lines of text from

6条回答
  •  我在风中等你
    2020-12-02 23:07

    It can be done in plain JS. The trick is to set scrollTop to a value equal or greater than the total height of the element (scrollHeight):

    const theDiv = document.querySelector('#thediv');
    theDiv.scrollTop = Math.pow(10, 10);
    

    From MDN:

    If set to a value greater than the maximum available for the element, scrollTop settles itself to the maximum value.

    While the value of Math.pow(10, 10) did the trick using a too high value like Infintiy or Number.MAX_VALUE will reset scrollTop to 0 (Firefox 66).

提交回复
热议问题