jquery button top to bottom

jQuery Button Top to Bottom

If you want to create a button in jQuery that scrolls to the bottom of the page, you can use the following code:


$('button').click(function() {
  $('html, body').animate({
    scrollTop: $(document).height()
  }, 'slow');
});

This code selects a button element using the jQuery selector and attaches a click event handler to it. When the button is clicked, the $('html, body') selector is used to select both the <html> and <body> elements, and the .animate() method is called with an object that specifies the scrollTop property should be set to the height of the document using $(document).height() to scroll down.

If you want to create a button that scrolls to the top of the page, you can use the following code:


$('button').click(function() {
  $('html, body').animate({
    scrollTop: 0
  }, 'slow');
});

This code is similar to the code above, but sets scrollTop to 0 to scroll up to the top of the page.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe