Javascript – Smooth scroll to top of page after an asp.net ajax postback

asp.netjavascript

I have an error message area at the top of a long page. After an ajax postback in asp.net (using an update panel), sometimes I need to display an error message and it will be displayed outisde the viewport and the user might not notice it.

I would like the return (same) page to scroll smoothly to the top of the page, rather than a sudden jump. It's a better user experience in my opinion.

I am looking for c# + Javascript code to do this effect or at least the JS smooth scrolling effect.

Best Answer

You can just use jQuery and scrollTo: http://demos.flesler.com/jquery/scrollTo/

Or just write the animation yourself:

$('body').animate({ scrollTop: 0 }, 1000);