jQuery .scrollTop() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

.scrollTop() gets or sets the vertical scroll offset in pixels — how much content is hidden above the visible area (since jQuery 1.2.6). At the top (or if not scrollable), the value is 0.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$el.scrollTop()        // getter → Number (first element)
$el.scrollTop(300)     // setter → jQuery (all matched)

$(window).scrollTop()  // page vertical scroll
$(window).scrollTop(0) // jump to top of page
CallWhat it does
.scrollTop()Learn jQuery .scrollTop(): get or set vertical scroll offset in pixels, window page scroll, vs .scrollLeft() and animate, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Pick a scrollable container (overflow: auto/scroll) or $(window).
Step 2Call .scrollTop() with no args to read the current offset.
Step 3Call .scrollTop(value) to jump to a vertical position.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Get the Vertical Offset

Official style: read scrollTop from the first paragraph.

jQuery
var p = $("p").first();
$("p").last().text("scrollTop:" + p.scrollTop());
// typically "scrollTop:0" for a normal paragraph
Try It Yourself

How It Works

  1. Select. $("p").first() is the element to measure.
  2. Getter. .scrollTop() returns a number (no args).
  3. Typical result. A normal paragraph is not scrolled → 0.

Example 2 — Set Scroll Position on a Box

Official style: jump a scrollable div to 300px.

jQuery
// Needs a tall child inside an overflow:auto box
$("div.demo").scrollTop(300);
console.log($("div.demo").scrollTop());  // ~300
Try It Yourself

How It Works

  1. Scrollable box. The container must overflow vertically.
  2. Setter. .scrollTop(300) hides 300px of content above.
  3. All matches. Every element in the set is updated.

Example 3 — Page Scroll With $(window)

Read or jump the browser window’s vertical scroll.

jQuery
var y = $(window).scrollTop();
console.log("pageY:", y);

$(window).scrollTop(0);  // jump to top
console.log("now:", $(window).scrollTop());
Try It Yourself

How It Works

  1. Window target. $(window) represents the page viewport.
  2. Read. Getter returns how far the page has scrolled down.
  3. Jump. .scrollTop(0) moves the page to the top.

Frequently Asked Questions

With no arguments, .scrollTop() returns the vertical scroll offset of the first matched element as a number of pixels hidden above the visible area. With a number, .scrollTop(value) sets that offset for every matched element and returns the jQuery object for chaining.
.scrollTop() is vertical (pixels hidden above). .scrollLeft() is horizontal (pixels hidden to the left). Both have been available since jQuery 1.2.6 and share the same getter/setter pattern.
Yes. $(window).scrollTop() reads how far the page has scrolled down, and $(window).scrollTop(0) jumps to the top. That is the usual pattern for sticky headers and back-to-top buttons.
Either the element is already at the top, or it is not vertically scrollable (content fits, or overflow is not set). On $(window), 0 means the page is at the very top.
Yes. Use $el.animate({ scrollTop: target }, duration) for a smooth scroll. For an instant jump, call .scrollTop(value) directly. Call .stop() first if users click repeatedly.
No. Elements with display:none have no reliable scroll layout. Show the element first, then read or set .scrollTop().

Next: DOM Methods

Continue with jQuery mouse event handlers and demos.

DOM Methods tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful