button click scroll to div code example

Example 1: 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 2: how can when click buton scrool to another elemtn

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

// example: http://jsfiddle.net/ryXFt/3/

Example 3: scroll to specific div

$(window).scroll(function() {
    $('html, body').animate({
        scrollTop: $("#myDiv").offset().top
    }, 2000);
});

Example 4: how to add scroll to div onclick

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

Tags:

Html Example