jQuery scrollLeft() Method

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

What Is scrollLeft()?

.scrollLeft() gets or sets the horizontal scroll position (since jQuery 1.2.6). The value is how many pixels of content are hidden to the left of the visible area — a unitless integer.

Remember
// Getter — first matched element only
$("div").scrollLeft()      // e.g. 0, 120, 300

// Setter — every matched element
$("div").scrollLeft(300)

// Animate
$("div").animate({ scrollLeft: 300 }, 400)
NeedUse
Horizontal offset$el.scrollLeft() / .scrollLeft(n)
Vertical offset$el.scrollTop()
Smooth move.animate({ scrollLeft: n })
Native propertyel.scrollLeft

How to Use scrollLeft()

Ensure the element can scroll horizontally, then get or set the offset.

  1. Use a visible container with overflow that allows horizontal scrolling.
  2. Make sure content is wider than the visible width.
  3. Call .scrollLeft() to read, or .scrollLeft(n) to jump.
  4. Optional: animate with .animate({ scrollLeft: n }).
PieceRequired?Role
Scrollable elementYesOverflow + wide content
value (setter)For setTarget horizontal pixels
Return (get)—Integer (first match)
Return (set)—Same jQuery object

Pseudocode

Pseudocode
if called with no args:
    return firstElement.scrollLeft   // pixels hidden on the left

if called with value:
    for each matched element:
        element.scrollLeft = value

// At far left or not scrollable → 0
// Hidden (display:none) → unreliable — show first

Cheat Sheet

Keep these facts handy for horizontal scroll.

TopicDetail
Get.scrollLeft() → integer
Set.scrollLeft(300)
SincejQuery 1.2.6
MeaningPixels hidden to the left
ZeroAt left edge or not scrollable
HiddenDoes not work on display: none
Animate.animate({ scrollLeft: n })

Live Preview

A small overflow box with wide content. Get, set, nudge, or reset scrollLeft and read the integer result.

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

Result
$box.scrollLeft()
→ 0

jQuery Examples

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

Example 1 — Get Current Offset

Read scrollLeft from the first matched element.

jQuery
// Overflow box with wide inner content (set up via .css in Try It)
var left = $("#box").scrollLeft();
console.log(left); // 0 at the far left
Try It Yourself

How It Works

  1. Select. $("#box") is the scrollable container.
  2. Get. No arguments means “read the current offset.”
  3. At left. Starting position is usually 0.
  4. First only. On a multi-element set, only the first is read.

Example 2 — Set Horizontal Position

Jump to 300px, then read the value back.

jQuery
$("#box").scrollLeft(300);
console.log($("#box").scrollLeft()); // 300
Try It Yourself

How It Works

  1. Set. Pass an integer pixel value.
  2. Effect. Content shifts so 300px is hidden on the left.
  3. Verify. A getter call returns 300.
  4. All matches. The setter updates every element in the collection.

Example 3 — Nudge Left and Right

Read the current value, add or subtract a step, then set.

jQuery
var $box = $("#box");
var step = 50;

$box.scrollLeft($box.scrollLeft() + step); // right
console.log($box.scrollLeft());

$box.scrollLeft($box.scrollLeft() - step); // left
console.log($box.scrollLeft());
Try It Yourself

How It Works

  1. Read. Current offset is the base.
  2. Add. + step scrolls content to the right.
  3. Subtract. - step scrolls back toward the left.
  4. Pattern. Classic read-modify-write for arrow buttons.

Example 4 — Animate scrollLeft

Tween the offset smoothly instead of jumping.

jQuery
$("#box").scrollLeft(0);
$("#box").animate({ scrollLeft: 200 }, 200, function () {
  console.log($("#box").scrollLeft()); // ~200
});
Try It Yourself

How It Works

  1. Reset. Start at 0 for a clear demo.
  2. Animate. jQuery tweens the scrollLeft property.
  3. Complete. The callback runs when the tween finishes.
  4. Use. Great for tab bars and carousels — still needs a visible element.

Edge Cases & Pitfalls

Check these before wiring horizontal scroll UI.

hidden

Hidden elements ignore scrollLeft

Show the element first. Same rule applies when animating scrollLeft.

zero

0 is ambiguous

It can mean “at the left” or “not scrollable.” Confirm overflow and content width.

overflow

Need a scrollable box

Without overflow-x: auto/scroll (or equivalent) and wide content, setters have no visible effect.

get

Getter uses the first match only

Narrow with .eq() or an id when several scroll boxes share a selector.

Browser Support

.scrollLeft() has been available since jQuery 1.2.6. It needs a browser DOM with jQuery loaded — not Node.js without a DOM.

✓ jQuery 1.2.6+ · Browsers only

jQuery .scrollLeft()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Relies on the native scrollLeft property under the hood.

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
.scrollLeft() Browsers

Bottom line: Use .scrollLeft() on visible, horizontally scrollable elements. Pair with .scrollTop() for vertical scroll.

Performance Notes

ApproachCostBest for
.scrollLeft(n)LowInstant jumps
.animate({ scrollLeft })Per-frameSmooth UX (tabs, carousels)
Native el.scrollLeftLowestHot paths without jQuery

Prefer instantaneous sets for frequent updates (drag). Use short animations for intentional navigation. Avoid reading scroll position inside tight loops without need.

Key Takeaways

  • Role: get/set horizontal scroll in pixels.
  • Meaning: pixels hidden to the left of the viewport.
  • Visible: does not work on display: none.
  • Pair: .scrollTop() for the vertical axis.

One line: .scrollLeft() reads or writes how far a box is scrolled horizontally — keep the element visible and scrollable.

Frequently Asked Questions

With no arguments it returns the horizontal scroll offset of the first matched element as a unitless integer (pixels hidden to the left). With a number argument it sets that offset on every matched element and returns the jQuery object for chaining.
.scrollLeft() is horizontal; .scrollTop() is vertical. Both return integers, share the same getter/setter pattern, and can be animated with .animate({ scrollLeft: n }) or .animate({ scrollTop: n }).
Hidden elements (display:none) have no scrollable layout box. Reading or animating scrollLeft on them does not work reliably. Show the element first, then read or set.
Either the bar is at the far left, or the element is not horizontally scrollable (content fits, or overflow does not allow scrolling). Check overflow and content width before assuming position.
Yes: $el.animate({ scrollLeft: target }, duration). For an instant jump, use .scrollLeft(value) instead.
For non-zero values, yes — the element needs a scrollable overflow and content wider than its visible width. Otherwise the getter stays 0 and the setter has no visible effect.

Next: scrollTop()

Learn how to get and set the vertical scroll offset with jQuery.

scrollTop() tutorial →

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