Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery scroll to top of page

Tags:

jquery

Is there a way to programmatically scroll to the top of a page with jQuery? I am currently trying to do this with the following code but it is not working. I am using Firefox currently,

$(window).scrollTop($(document).height()); 
like image 831
medium Avatar asked Feb 16 '10 18:02

medium


People also ask

How do you scroll automatically to the top of the page using JavaScript?

Method 1: Using window.scrollTo() The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.

How do I scroll to the top of the page in HTML?

window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.

What is jQuery scrollTop?

jQuery scrollTop() Method The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.

What is scrollTop?

An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .


1 Answers

Try this:

$('html, body').animate({scrollTop: '0px'}, 300); 

You can do this with 0 instead of 300 to be instant, but this gives a quick auto-scroll effect.

like image 168
Nick Craver Avatar answered Sep 23 '22 06:09

Nick Craver