Scrolling Overflowed DIVs with JavaScript

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

    scrollIntoView

    The scrollIntoView method scrolls the element into view.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 22:53

    I had a div wrapping 3 divs that were floating left, and whose contents were being resized. It helps to turn funky-colored borders/background on for the div-wrapper when you try to resolve this. The problem was that the resized div-content was overflowing outside the div-wrapper (and bled to underneath the area of content below the wrapper).

    Resolved by using @Shog9's answer above. As applied to my situation, this was the HTML layout:

    <div id="div-wrapper">
      <div class="left-div"></div>
      <div id="div-content" class="middle-div">
      Some short/sweet content that will be elongated by Jquery.
      </div>
      <div class="right-div"></div>
    </div>
    

    This was the my jQuery to resize the div-wrapper:

    <script>
    $("#div-content").text("a very long string of text that will overflow beyond the width/height of the div-content");
    //now I need to resize the div...
    var contentHeight = $('#div-content').prop('scrollHeight')
    $("#div-wrapper").height(contentHeight);
    </script>
    

    To note, $('#div-content').prop('scrollHeight') produces the height that the wrapper needs to resize to. Also I am unaware of any other way to obtain the scrollHeight an actual jQuery function; Neither of $('#div-content').scrollTop() and $('#div-content').height would produce the real content-height values. Hope this helps someone out there!

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-12-02 23:04
    $("#thediv").scrollTop($("#thediv")[0].scrollHeight);
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题