Cross-Browser method for setting ScrollTop of an element?

后端 未结 3 1213
半阙折子戏
半阙折子戏 2020-12-31 04:31

For example I have I a div tag with the scroll bar on the right of the div tag.
I want to show the div with the last line, so I have this:

document.get         


        
相关标签:
3条回答
  • 2020-12-31 05:11

    Make sure that overflow property is set:

    <div id="divscroll" style="height: 100px; width: 100px; overflow: scroll;">
     //// something something 
    </div>
    
    0 讨论(0)
  • 2020-12-31 05:15

    scrollTop works in all major browsers.

    To scroll to the bottom of the element:

    var div = document.getElementById('divscroll');
    div.scrollTop = div.scrollHeight - div.clientHeight;
    

    clientHeight also works across browsers, and scrollHeight mostly works.

    0 讨论(0)
  • 2020-12-31 05:17

    Try setting the scroll position to a real figure, instead of just an arbitrary big number:

    document.getElementById("divscroll").scrollTop = document.getElementById("divscroll").scrollHeight; 
    
    0 讨论(0)
提交回复
热议问题