Scrolling Overflowed DIVs with JavaScript

前端 未结 6 533
逝去的感伤
逝去的感伤 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:52

    scrollHeight should be the total height of content. scrollTop specifies the pixel offset into that content to be displayed at the top of the element's client area.

    So you really want (still using jQuery):

    $("#thediv").each( function() 
    {
       // 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(this.scrollHeight, this.clientHeight);
       this.scrollTop = scrollHeight - this.clientHeight;
    });
    

    ...which will set the scroll offset to the last clientHeight worth of content.

提交回复
热议问题