scroll page on click jquery code example

Example 1: scroll to element jquery

$('html, body').animate({
  scrollTop: $("#grepperRocks").offset().top
});

Example 2: jquery scroll to element

$("#button").click(function() {
    $([document.documentElement, document.body]).animate({
        scrollTop: $("#elementtoScrollToID").offset().top
    }, 2000);
});

Example 3: how to add scroll to div onclick

<div class="first"><button type="button" onclick="smoothScroll(document.getElementById('second'))">Click Me!</button></div>
<div class="second" id="second">Hi</div>

Example 4: how to add scroll to div onclick

$("button").click(function() {
    $('html,body').animate({
        scrollTop: $(".second").offset().top},
        'slow');
});

Tags:

Css Example