Scroll Automatically to the Bottom of the Page

后端 未结 24 2892
佛祖请我去吃肉
佛祖请我去吃肉 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 05:54

    jQuery isn't necessary. Most of the top results I got from a Google search gave me this answer:

    window.scrollTo(0,document.body.scrollHeight);
    

    Where you have nested elements, the document might not scroll. In this case, you need to target the element that scrolls and use it's scroll height instead.

    window.scrollTo(0,document.querySelector(".scrollingContainer").scrollHeight);

    You can tie that to the onclick event of your question (i.e.

    ).

    Some additional sources you can take a look at:

    • http://www.sourcetricks.com/2010/07/javascript-scroll-to-bottom-of-page.html
    • http://www.alecjacobson.com/weblog/?p=753
    • http://www.mediacollege.com/internet/javascript/page/scroll.html
    • http://www.electrictoolbox.com/jquery-scroll-bottom/

提交回复
热议问题