How can I scroll to an element using jQuery?

JavaScript answer

If you would like to scroll to an element smoothly with “pure JavaScript” you could do something like this:

document.querySelector('#menudiv').scrollIntoView({
    behavior: 'smooth'
});

More information is here: Element.scrollIntoView()

NOTE: Please see Can I use… Support tables for HTML5, CSS3, etc. for the latest browser support…

jQuery answer

If you would like to scroll to an element smoothly using jQuery then you could do something like:

$(document).ready(function() {
  $(".btns").click(function() {
    $("html").animate(
      {
        scrollTop: $("#menudiv").offset().top
      },
      800 //speed
    );
  });
});

Leave a Comment