How to Scroll to an Element With Jquery?
To scroll to an element with JQuery, you can use the scrollTop
method and the offset
method to determine the element's position on the page. Here is an example:
$(document).ready(function(){
$('#button').click(function(){
$('html, body').animate({
scrollTop: $('#element').offset().top
}, 1000); //scroll to the element over a period of 1 second (1000ms)
});
});
In this example, the #button
element is clicked, triggering the scrolling action. The $('html, body').animate()
method animates the scrolling action by gradually changing the scrollTop
value over a period of time. The $('#element').offset().top
value is used to determine the position of the target element on the page.