How to scroll to the top of the page with JQuery


You may have seen an up arrow icon or button shown at the bottom of the page on many websites. Clicking this button takes user to the top of the page with some smooth scrolling animation. There are many ways to achieve this functionality as shown in following code snippet.

To quickly jump to the top of the page, you can use scrollTo function of window object

window.scrollTo(x-coord, y-coord);

Parameters

x-coord is the pixel along the horizontal axis.
y-coord is the pixel along the vertical axis.

If you want smooth scrolling, try something like this:

$("html, body").animate({ scrollTop: 0 }, "slow");

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments