jQuery scrollLeft() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
CSS

Definition

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

Syntax

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

jQuery
// 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)
CallWhat it does
scrollLeft()Learn jQuery .scrollLeft(): get or set horizontal scroll offset in pixels, animate scrollLeft, compare with scrollTop, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Use a visible container with overflow that allows horizontal scrolling.
Step 2Make sure content is wider than the visible width.
Step 3Call .scrollLeft() to read, or .scrollLeft(n) to jump.

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

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.

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.

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() Method

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

.scrollTop() Method 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