jQuery .scrollTop() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Vertical scroll

What You’ll Learn

The .scrollTop() method gets or sets the vertical scroll offset of matched elements as a unitless pixel Integer — ideal for page scroll, vertical panels, and back-to-top buttons. This tutorial covers getter and setter overloads, comparisons with .scrollLeft(), native element.scrollTop and window.pageYOffset, $(window).scrollTop() for page scroll, scroll event handling, read-modify-write button patterns, and animated vertical scrolling.

01

Getter

.scrollTop() → Integer

02

Setter

Number (pixels)

03

vs scrollLeft

Vertical twin

04

Animate

{ scrollTop }

05

Overflow

auto / scroll

06

Since 1.2.6

Core API

Introduction

After learning horizontal scroll with .scrollLeft(), the natural next step is vertical scroll. jQuery’s .scrollTop() returns a plain integer you can read, increment, or pass directly to .animate() — without parsing CSS strings.

Available since jQuery 1.2.6, .scrollTop() works as both a getter (no arguments) and a setter (pixel value). The getter reads the vertical scroll offset of the first matched element — how many pixels of content are hidden above the visible area. The setter scrolls every element in the set to that position. Unlike .scrollLeft(), which is mainly used on overflow containers, .scrollTop() also works on $(window) and $(document) for page-level scroll.

Understanding the .scrollTop() Method

Given a jQuery object, .scrollTop() either returns the current vertical scroll offset of the first matched element as a unitless pixel Integer (getter), or sets the scroll position on every matched element (setter). The value represents pixels of content hidden above the visible area — 0 when flush at the top or when the element is not scrollable.

Think of .scrollTop() as a vertical window into overflowing content. .scrollLeft() is the horizontal twin. Native element.scrollTop (or window.pageYOffset for the page) does the same at the DOM level; jQuery adds collection semantics and chaining. Use .css() for styling overflow — not for reading the current scroll offset.

💡
Beginner Tip

var y = $("div").scrollTop() gives you an Integer like 120 — pixels scrolled from the top edge. For smooth back-to-top motion, use $("div").animate({ scrollTop: 0 }, 400) instead of jumping with the setter.

📝 Syntax

jQuery .scrollTop() has two argument forms — one getter and one setter:

Get — no arguments (since 1.2.6)

jQuery
.scrollTop()  // -> Integer
  • Returns the vertical scroll offset of the first matched element as a unitless pixel Integer.
  • Reports pixels hidden above — 0 at the top edge or when not scrollable.
  • Native equivalent: element.scrollTop; for the page: window.pageYOffset.

Set — Number (since 1.2.6)

jQuery
.scrollTop( value )  // -> jQuery
  • value — vertical scroll position in pixels (e.g. 300).
  • Applied to every matched element in the set — including $(window).
  • Returns the original jQuery object for chaining — e.g. .scrollTop(300).css("borderColor", "green").

Animate — smooth scroll (since 1.2.6)

jQuery
.animate({ scrollTop: value }, duration )
  • Tweens the scroll offset over time — ideal for back-to-top buttons and feed panels.
  • Read current position with .scrollTop(), compute target, then animate.
  • Returns the jQuery object; use a completion callback for post-scroll actions.

Return value

  • Getter — unitless Integer (pixels). May be 0 when not scrollable.
  • Setter — the original jQuery object for chaining.

Important note

⚠️
Hidden elements

.scrollTop() does not work on display:none elements — jQuery cannot read or set a meaningful scroll offset on hidden nodes. Show the element before measuring or scrolling.

Official jQuery API example

jQuery
var p = $( "p" ).first();
$( "p" ).last().text( "scrollTop:" + p.scrollTop() );

⚡ Quick Reference

GoalCode
Read vertical scroll (Integer)$("div").scrollTop()
Scroll to pixel position$("div").scrollTop(300)
Read page scroll position$(window).scrollTop()
Smooth vertical scroll$("div").animate({ scrollTop: 300 }, 400)
Back to top (animated)$("html, body").animate({ scrollTop: 0 }, 400)
Scroll by delta (read-modify-write)$el.scrollTop($el.scrollTop() + 50)
Horizontal counterpart$("div").scrollLeft()
Native equivalentelement.scrollTop / window.pageYOffset
Listen for scroll changes$("div").on("scroll", fn)
Enable scrolling (CSS)$("div").css("overflow-y", "auto")

📋 .scrollTop() vs .scrollLeft() vs native scrollTop vs $(window).scrollTop()

Four ways to work with scroll — vertical offset, horizontal twin, native DOM, and page-level scroll.

.scrollTop()
scroll Y

Get/set vertical scroll — returns unitless Integer (getter)

.scrollLeft()
scroll X

Horizontal counterpart — same getter/setter/animate pattern

native
el.scrollTop

DOM property on a single element — page: window.pageYOffset

$(window)
page scroll

Read/set document scroll — sticky headers, scroll-spy, back-to-top

Examples Gallery

Examples 1–2 follow the official jQuery API documentation. Examples 3–5 handle scroll events, read-modify-write buttons, and animated back-to-top. Use DevTools or the Try-it links to run each snippet.

📚 Getting Started

Official jQuery demos for reading and setting vertical scroll.

Example 1 — Official Demo: Get Paragraph scrollTop

Read the vertical scroll offset of the first <p> inside a scrollable container and display it in the last paragraph.

jQuery
var p = $( "p" ).first();
$( "p" ).last().text( "scrollTop:" + p.scrollTop() );
Try It Yourself

How It Works

No-argument .scrollTop() returns a unitless pixel Integer — how many pixels of content are hidden above the visible area. On load the value is typically 0 when the container is at the top edge.

Example 2 — Official Demo: Set scrollTop to 300

Scroll a tall demo div vertically to pixel position 300 on load.

jQuery
$( "div.demo" ).scrollTop( 300 );
Try It Yourself

How It Works

.scrollTop(300) sets the vertical scroll position instantly. The setter returns the jQuery object for chaining. The container must have overflow-y: auto or scroll and content taller than the viewport.

📈 Practical Patterns

Scroll events, button controls, and animated back-to-top navigation.

Example 3 — Display scrollTop on Scroll Event

Attach a scroll handler and show the live vertical offset as the user scrolls.

jQuery
$( "#panel" ).on( "scroll", function() {
  $( "#out" ).text( "scrollTop: " + $( this ).scrollTop() );
});
Try It Yourself

How It Works

The native scroll event fires whenever the scroll offset changes. Inside the handler, $(this).scrollTop() reads the current vertical position — the foundation for scroll progress bars and infinite-scroll detection.

Example 4 — Up/Down Buttons: Read-Modify-Write scrollTop

Increment or decrement scroll position by 50 pixels per button click.

jQuery
var step = 50;
$( "#up" ).on( "click", function() {
  var $el = $( "#panel" );
  $el.scrollTop( $el.scrollTop() - step );
});
$( "#down" ).on( "click", function() {
  var $el = $( "#panel" );
  $el.scrollTop( $el.scrollTop() + step );
});
Try It Yourself

How It Works

This read-modify-write pattern is the standard way to scroll by a fixed step: getter returns current offset, you adjust the number, setter applies the new position. jQuery clamps at 0 at the top — it will not scroll into negative territory.

Example 5 — Animate Back to Top: { scrollTop: 0 }

Click a button to smoothly scroll a feed panel back to the top.

jQuery
$( "#feed" ).scrollTop( 400 );
$( "#topBtn" ).on( "click", function() {
  $( "#feed" ).stop().animate({ scrollTop: 0 }, 400 );
});
Try It Yourself

How It Works

.animate({ scrollTop: 0 }, 400) tweens the vertical scroll offset smoothly. Call .stop() first if the user may click repeatedly. The same pattern works on $("html, body") for full-page back-to-top buttons.

🚀 Common Use Cases

  • Back-to-top buttons.animate({ scrollTop: 0 }, duration) on $(window) or a scrollable panel.
  • Page scroll position$(window).scrollTop() for sticky headers, scroll-spy, and parallax triggers.
  • Vertical panels and feeds — chat windows, comment threads, and notification lists.
  • Custom up/down controls — read-modify-write with up/down buttons (see Example 4).
  • Scroll progress indicatorsscroll event + .scrollTop() to update a vertical progress bar.
  • Infinite scroll and lazy load — detect when scrollTop + innerHeight nears content height to fetch more items.

🧠 How .scrollTop() Reads and Writes Scroll Offset

1

Match container

jQuery object holds scrollable DOM elements — or $(window) for page scroll.

Input
2

Detect mode

No args → getter. Number arg → setter on each element.

Route
3

Read or write

Getter reads native scrollTop (or page offset). Setter assigns pixel offset. Requires visible, scrollable layout.

DOM
4

Return result

Getter → unitless pixel Integer. Setter → same jQuery object for chaining.

📝 Notes

  • Available since jQuery 1.2.6 — vertical scroll counterpart of .scrollLeft().
  • Getter returns a unitless pixel Integer from the first matched element only.
  • Setter applies to all matched elements and returns the jQuery object for chaining.
  • Also works on $(window) and $(document) for page scroll — unlike .scrollLeft().
  • Requires overflow-y: auto, scroll, or content taller than the container for meaningful values.
  • Does not work on hidden (display:none) elements.
  • 0 means at the top edge or not scrollable — check overflow before assuming.
  • Animatable via .animate({ scrollTop: value }, duration).
  • Native equivalent: element.scrollTop / window.pageYOffset — jQuery adds collection semantics.

Browser Support

.scrollTop() has been part of jQuery since 1.2.6+. It wraps the native scrollTop property with no browser-specific quirks beyond jQuery itself. Animation via .animate({ scrollTop }) works wherever jQuery animation is supported.

jQuery 1.2.6+

jQuery .scrollTop()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: element.scrollTop / window.pageYOffset — jQuery adds collection semantics, getter/setter overloads, and animate integration.

100% With jQuery loaded
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() Universal

Bottom line: Safe in any jQuery project. Use .scrollTop() for vertical scroll math; use $(window).scrollTop() for page scroll; use .css('overflow-y') to enable scrolling.

Conclusion

The jQuery .scrollTop() method gets or sets vertical scroll offset as a unitless pixel Integer — the go-to helper when you need to read, jump, or animate vertical scroll inside overflow containers or on the page via $(window). It complements .scrollLeft() for horizontal scroll and native element.scrollTop / window.pageYOffset for single-element access.

Remember the official demo: var p = $( "p" ).first(); $( "p" ).last().text( "scrollTop:" + p.scrollTop() ); reads the offset, and $( "div.demo" ).scrollTop( 300 ) jumps to a position. Pair .scrollTop() with scroll events for live feedback, read-modify-write for buttons, and .animate({ scrollTop: 0 }) for smooth back-to-top.

💡 Best Practices

✅ Do

  • Use .scrollTop() when you need an Integer for scroll math or .animate()
  • Use $(window).scrollTop() for page scroll position and back-to-top on the document
  • Ensure overflow-y: auto or scroll on containers with tall content
  • Use read-modify-write for step buttons: $el.scrollTop($el.scrollTop() + step)
  • Call .stop() before .animate({ scrollTop: 0 }) when users may click repeatedly

❌ Don’t

  • Call .scrollTop() on display:none elements — it will not work
  • Assume 0 always means “at top edge” — non-scrollable containers also return 0
  • Use .css() to read scroll position — it cannot report current offset
  • Expect the getter to read all matched elements — only the first
  • Forget .scrollLeft() when you need horizontal scroll — they are independent axes

Key Takeaways

Knowledge Unlocked

Six things to remember about .scrollTop()

Vertical scroll — ready for math and animation.

6
Core concepts
= 02

Setter

Pixels

Write
X 03

scrollLeft

Horizontal

Compare
fx 04

Animate

Smooth

Motion
win 05

$(window)

Page scroll

Document
hide 06

Hidden

No-op

Caveat

❓ Frequently Asked Questions

Calling .scrollTop() with no arguments is the getter — it returns the current vertical scroll offset of the first matched element as a unitless Integer (pixels hidden above the visible area). Calling .scrollTop(value) is the setter — it scrolls every matched element to that vertical position and returns the original jQuery object for chaining. The getter reads only the first element; the setter applies to all elements in the set.
.scrollTop() measures and sets vertical scroll — how many pixels of content are hidden above the visible area inside an overflow container. .scrollLeft() is the horizontal counterpart — pixels hidden to the left. Both return Integers, work since jQuery 1.2.6, fail on hidden elements, and support .animate({ scrollTop: value }) or .animate({ scrollLeft: value }). Use .scrollTop() for page scroll, vertical panels, and back-to-top buttons; use .scrollLeft() for carousels, tab bars, and wide tables.
Yes — .scrollTop() also works on $(window) and $(document) to read or set the page scroll position, unlike .scrollLeft() which is mainly used on scrollable elements. $(window).scrollTop() returns how far the page has scrolled down; $(window).scrollTop(0) jumps to the top. The native equivalent for the window is window.pageYOffset (read) or window.scrollTo(0, value). This makes .scrollTop() one of the most common jQuery methods for sticky headers, scroll-spy, and back-to-top buttons.
Elements with display:none (or otherwise not rendered) have no scrollable layout box — the browser cannot compute or apply a meaningful scroll offset. jQuery delegates to the native scrollTop property, which is unreliable or zero on hidden nodes. Show the element first, or measure after making it visible, before reading or setting .scrollTop(). The same limitation applies to .scrollLeft().
A getter result of 0 means either the element is scrolled all the way to the top (no content hidden above) or the element is not vertically scrollable. Non-scrollable containers — those without overflow: auto or overflow: scroll and whose content fits within the visible height — always report 0 because there is nothing to scroll. Do not assume 0 always means 'at the top edge'; check overflow and content height first. On $(window), 0 means the page is at the very top.
Yes — pass scrollTop as an animation property: $container.animate({ scrollTop: 0 }, duration) smoothly scrolls back to the top. jQuery tweens the scroll offset over time, which is ideal for chat feeds, long articles, and panel navigation. Read the current value with .scrollTop(), compute a target, then animate. For instant jumps without transition, use the setter .scrollTop(value) directly. Combine with .stop() before animating if the user clicks back-to-top repeatedly.
Did you know?

jQuery’s .scrollTop() has existed since version 1.2.6 — the vertical twin of .scrollLeft(). Both wrap the native DOM scroll properties so you can call them on jQuery collections and chain setters. Unlike .scrollLeft(), which is mainly used on overflow containers, .scrollTop() is also commonly called on $(window) to read page scroll — the jQuery equivalent of window.pageYOffset. You can animate either axis with .animate({ scrollTop: n }) or .animate({ scrollLeft: n }) — a pattern that predates CSS scroll-behavior: smooth by many years. For back-to-top buttons, $("html, body").stop().animate({ scrollTop: 0 }, 400) remains a reliable cross-browser approach.

Next: Mouse Events

Learn how to bind and trigger click handlers with .on() and .trigger().

Mouse Events hub →

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.

6 people found this page helpful