I’m trying to scroll automatically to a specific element when clicking a button. I know this can be done using jQuery, but I’m unsure how. Can someone help me figure out how to scroll to an element using jQuery?
To scroll to an element with jQuery, you can use the animate
method and scrollTop
property.
$("button").click(function() {
$('html, body').animate({
scrollTop: $("#element-id").offset().top
}, 2000);
});
You can use the example below aswell.
$([document.documentElement, document.body]).animate({
scrollTop: $("#element-id").offset().top
}, 2000);