Scroll a page up and down with jQuery

Sometimes it’s useful to scroll a page up or down automatically to send users to certain parts of the page. One way we can easily accomplish that is by using the scrollTop function from jQuery.

Code example:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script language="JavaScript">
    function scroll_top()
    {
        $(window).scrollTop(0);
    }
    function scroll_to_last_div()
    {
        $(window).scrollTop($('#container div:last').offset().top);
    }
    </script>
</head>
<body>
    <div id="container" style="display:block; width:400px; margin: 0 auto; text-align: center;">
        <a href="javascript:scroll_to_last_div();">Scroll to last div!</a>
        <div style="display: block; height: 600px; width: 400px; background: #234a34;"></div>
        <a href="javascript:scroll_top();">Scroll to top!</a>
        <div style="display: block; height: 600px; width: 400px; background: #fa342a;"></div>
    </div>
</body>
</html>