Scroll Automatically to the Bottom of the Page

后端 未结 24 2970
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 05:22

Consider I have a list of questions. When I click on the first question, it should automatically take me to the bottom of the page.

For a matter of fact, I do know

24条回答
  •  借酒劲吻你
    2020-11-22 06:07

    If you want to scroll entire page to the bottom:

    var scrollingElement = (document.scrollingElement || document.body);
    scrollingElement.scrollTop = scrollingElement.scrollHeight;
    

    See the sample on JSFiddle

    If you want to scroll an element to the bottom:

    function gotoBottom(id){
       var element = document.getElementById(id);
       element.scrollTop = element.scrollHeight - element.clientHeight;
    }
    

    And that's how it works:

    Ref: scrollTop, scrollHeight, clientHeight

    UPDATE: Latest versions of Chrome (61+) and Firefox does not support scrolling of body, see: https://dev.opera.com/articles/fixing-the-scrolltop-bug/

提交回复
热议问题