Scrolling Overflowed DIVs with JavaScript

前端 未结 6 542
逝去的感伤
逝去的感伤 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 22:56

    Using a loop to iterate over a jQuery of one element is quite inefficient. When selecting an ID, you can just retrieve the first and unique element of the jQuery using get() or the [] notation.

    var div = $("#thediv")[0];
    
    // certain browsers have a bug such that scrollHeight is too small
    // when content does not fill the client area of the element
    var scrollHeight = Math.max(div.scrollHeight, div.clientHeight);
    div.scrollTop = scrollHeight - div.clientHeight;
    

提交回复
热议问题