jQuery .scrollTop() Method

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

What Is .scrollTop()?

.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.

Remember
$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
NeedUse
Vertical scroll$el.scrollTop() / .scrollTop(n)
Horizontal scroll$el.scrollLeft()
Smooth scroll$el.animate({ scrollTop: n })

How to Use .scrollTop()

Select a scrollable element (or the window), then get or set the offset.

  1. Pick a scrollable container (overflow: auto/scroll) or $(window).
  2. Call .scrollTop() with no args to read the current offset.
  3. Call .scrollTop(value) to jump to a vertical position.
  4. Optionally animate with .animate({ scrollTop: value }).
PieceRequired?Role
jQuery selectionYesElement(s) or window / document
valueSetter onlyNew vertical position in pixels
Return—Getter → Number; setter → jQuery (chainable)

Pseudocode

Pseudocode
function scrollTop(selection, value):
    if value is missing:
        return firstElement.verticalScrollPixels   // or 0

    for each el in selection:
        el.verticalScrollPixels = value
    return selection

Cheat Sheet

Keep these facts handy for vertical scrolling.

TopicDetail
Syntax.scrollTop() / .scrollTop(value)
SincejQuery 1.2.6
Getter unitPixels (unitless number)
At top / not scrollable0
Horizontal cousin.scrollLeft()
Native cousinselement.scrollTop, window.scrollY

Live Preview

Scroll the box, then read or set .scrollTop().

Line 1 — scroll me

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10 — bottom

Browser demo of .scrollTop() — same idea as the examples below.

Result
scrollTop: 0

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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.
  4. Display. Write that value into the last paragraph’s text.

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.
  4. Verify. Call the getter again to confirm the new offset.

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.
  4. Use case. Sticky headers, scroll-spy, and back-to-top buttons.

Example 4 — Animate Back to Top

Smoothly scroll a container (or the window) to 0.

jQuery
$("#panel").scrollTop(200);

$("#panel").stop().animate({ scrollTop: 0 }, 400, function () {
  console.log("at top:", $("#panel").scrollTop());
});
Try It Yourself

How It Works

  1. Start offset. Jump to 200 so there is something to animate.
  2. Animate. .animate({ scrollTop: 0 }) tweens the offset.
  3. Stop. .stop() clears a previous animation if the user clicks again.
  4. Done. The callback runs when the panel reaches the top.

Edge Cases & Pitfalls

Check these before wiring scroll UI.

overflow

Needs a scrollable box

Without overflow: auto/scroll and taller content, the getter stays 0 and the setter has no visible effect.

first

Getter uses the first match only

The setter updates every matched element; the getter reads only the first.

hidden

Hidden elements fail

display:none elements are not in the scroll layout — measure after showing them.

native

Native equivalents

Outside jQuery, use element.scrollTop or window.scrollY / window.scrollTo.

Browser Support

.scrollTop() has shipped since jQuery 1.2.6 and works in all modern browsers with jQuery loaded. It maps to the native scroll offset APIs under the hood.

✓ jQuery 1.2.6+

.scrollTop()

Supported everywhere jQuery runs in the browser. Use it on scrollable elements or $(window) for page scroll.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.scrollTop() Excellent

Bottom line: Call .scrollTop() to read and .scrollTop(n) to set. Prefer .animate({ scrollTop: n }) for smooth motion.

Performance Notes

ApproachCostBest for
.scrollTop() / .scrollTop(n)CheapInstant jumps, one-off reads
.animate({ scrollTop })FramesSmooth UX / back-to-top
Scroll event spamHighThrottle or debounce listeners

Reading or setting scroll is inexpensive. Cost spikes when you attach heavy work to every scroll event — throttle those handlers.

Key Takeaways

  • Role: get/set vertical pixels hidden above the view.
  • Window: $(window).scrollTop() for page scroll.
  • Smooth: animate scrollTop instead of jumping.
  • Horizontal: use .scrollLeft().

One line: .scrollTop() reads vertical offset; .scrollTop(n) sets it — use $(window) for the page.

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: Mouse Events

Continue with jQuery mouse event handlers and demos.

Mouse events →

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