jquery scroll to div code example

Example 1: smooth scroll jquery onclick

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

Example 2: scroll to element jquery

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

Example 3: jquery scroll to top of div

$('#DIV_ID').scrollTop(0);

Example 4: jquery scroll to id

$('html, body').animate(
    {
      scrollTop: $($(this).attr('href')).offset().top,
    },
    500,
    'linear'
  )

Example 5: jquery scroll to element

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

Example 6: javascript go to div id

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

window.onload = scroll;