JavaScript Element scrollBy() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

Element.scrollBy() is an instance method from the CSSOM View module. It scrolls an element by a given amount relative to where it is now. Learn delta scrolling, behavior: "smooth", the Promise interrupted flag, and five try-it labs.

01

Kind

Instance method

02

Action

Scroll by delta

03

Vs scroll()

Relative not absolute

04

Returns

Promise

05

behavior

smooth / instant

06

Status

Baseline widely

Introduction

Carousels, image galleries, and long panels often need a small nudge—not a jump to pixel 1000. Call scrollBy() to move relative to the current scroll position by a number of pixels.

MDN: scrollBy(x, y) adds to the current offset. Like scroll(), modern browsers return a Promise with { interrupted } so you can run code when a smooth scroll finishes (or was cut short).

💡
Beginner tip

Think of scroll() as “go to this spot” and scrollBy() as “move this many pixels from here.” Use negative values to scroll up or left.

This page is part of JavaScript Element. Related: scroll(), scrollTop, and the JavaScript hub.

Understanding the scrollBy() Method

Calling el.scrollBy(x, y) or el.scrollBy({ top, left, behavior }) adds the given pixel amounts to the element’s current scroll offset.

  • It is an instance method on Element (CSSOM View).
  • Scrolls relative to the current position (MDN).
  • Returns a Promise with { interrupted: boolean } (MDN).
  • Options: top, left, behavior (smooth, instant, auto).
  • Negative deltas scroll up or left; positive scroll down or right.
  • Pair with scroll() when you need absolute coordinates.

📝 Syntax

General forms of Element.scrollBy (MDN):

JavaScript
scrollBy(xCoord, yCoord)
scrollBy(options)

Parameters

  • xCoord, yCoord — horizontal and vertical pixels to scroll by (MDN).
  • options (object):
    • top — Y-axis delta in pixels
    • left — X-axis delta in pixels
    • behavior"smooth", "instant", or "auto" (default)

Return value

A Promise that fulfills with { interrupted: boolean } (MDN).

Exceptions

  • No standard exceptions documented; invalid targets may simply not scroll.

Common patterns

JavaScript
const panel = document.getElementById("panel");

panel.scrollBy(0, 200);                   // MDN: nudge down 200px
panel.scrollBy({ top: 100, left: 50, behavior: "smooth" });

const result = await panel.scrollBy(0, -100);
console.log(result.interrupted);          // false if scroll finished

⚡ Quick Reference

GoalCode
Scroll by deltael.scrollBy(0, 200)
Smooth nudgeel.scrollBy({ top: 100, behavior: "smooth" })
Read positionel.scrollTop / el.scrollLeft
Absolute jumpel.scroll(0, 1000)
Return valuePromise<{ interrupted }>
MDN statusBaseline Widely available (since January 2020)

🔍 At a Glance

Four facts to remember about Element.scrollBy().

Returns
Promise

{ interrupted }

Baseline
widely

Since Jan 2020

Delta
relative

Not absolute

behavior
smooth

Optional animate

📋 scrollBy() vs scroll() vs scrollTop

scrollBy()scroll()scrollTop
Position typeRelative deltaAbsolute coordsProperty (read/write)
AliasNonescrollTo()N/A
behavior optionYesYesNo (instant)
Promise returnYes (MDN)Yes (MDN)No
Best forNudge by pixelsJump to sectionSimple read/set

Examples Gallery

Examples follow MDN Element.scrollBy() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN relative deltas and smooth scrolling.

Example 1 — Basic scrollBy(300, 300) (MDN)

Scroll an element 300 pixels right and 300 pixels down from its current position.

JavaScript
const element = document.getElementById("panel");

// Scroll 300px right and 300px down from here
element.scrollBy(300, 300);
Try It Yourself

How It Works

MDN: the first argument is horizontal delta, the second is vertical delta. Values are added to the current scroll position, not set as absolute coordinates.

Example 2 — Options With behavior: "smooth" (MDN)

Scroll relatively with an options object for animated movement.

JavaScript
element.scrollBy({
  top: 100,
  left: 100,
  behavior: "smooth",
});
Try It Yourself

How It Works

behavior: "smooth" animates the relative scroll. You can also set CSS scroll-behavior: smooth on the element and use behavior: "auto".

📈 Practical Patterns

Page-down buttons, Promise results, and horizontal carousels.

Example 3 — Page Down scrollBy(0, 200) (MDN Demo)

Nudge a scrollable section down by 200 pixels—like MDN’s toolbar demo.

JavaScript
document.querySelector(".scroll-by").addEventListener("click", async () => {
  const section = document.querySelector("section");
  const result = await section.scrollBy(0, 200);
  console.log("interrupted:", result.interrupted);
});
Try It Yourself

How It Works

MDN’s element methods demo uses scrollBy(0, 200) on a <section> with scroll-behavior: smooth in CSS.

Example 4 — Promise interrupted (MDN)

Detect when a smooth scroll was cut short by another scroll.

JavaScript
async function nudgeAndReport(el, dy) {
  const result = await el.scrollBy({ top: dy, behavior: "smooth" });
  console.log(result.interrupted ? "Interrupted" : "Finished");
}
Try It Yourself

How It Works

MDN: if you trigger a second scroll before the first smooth scrollBy() completes, the promise resolves with interrupted: true.

Example 5 — Horizontal Carousel scrollBy(200, 0)

Scroll a wide row of cards left or right by a fixed step.

JavaScript
const track = document.getElementById("carousel");

document.getElementById("next").addEventListener("click", () => {
  track.scrollBy({ left: 200, behavior: "smooth" });
});

document.getElementById("prev").addEventListener("click", () => {
  track.scrollBy({ left: -200, behavior: "smooth" });
});
Try It Yourself

How It Works

Horizontal galleries use overflow-x: auto and scrollBy({ left }) for prev/next buttons without calculating absolute positions.

🚀 Common Use Cases

  • Page-down / page-up buttons inside a scrollable panel.
  • Carousel prev/next controls with scrollBy({ left }).
  • Incremental reading apps that advance by one screen height.
  • Animating small nudges with behavior: "smooth".
  • Syncing UI after scroll completes via the Promise interrupted flag.
  • Teaching absolute vs relative scrolling (scroll vs scrollBy).

🧠 How scrollBy() Moves Content

1

Pick a scrollable element

The element needs overflow content (overflow: auto/scroll).

Target
2

Call scrollBy with deltas

Pass (x, y) or { top, left, behavior } as pixel offsets (MDN).

Request
3

Browser adds to scroll offset

scrollTop / scrollLeft increase or decrease by the delta.

Scroll
4

Promise resolves with interrupted

Run follow-up UI when smooth scrolling finishes (MDN).

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available since January 2020).
  • scrollBy() scrolls relative to the current position (MDN).
  • Returns a Promise with { interrupted: boolean } (MDN).
  • Use scroll() for absolute positions; scrollBy() for deltas.
  • behavior: "smooth" animates; pair with CSS scroll-behavior when using auto.
  • Related: scroll(), scrollTop, scrollHeight, JavaScript hub.

Browser Support

Element.scrollBy() is Baseline Widely available (MDN: across browsers since January 2020). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.scrollBy()

Safe for production. Scroll an element by a relative delta with optional smooth behavior.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Legacy scrollTop only · no Promise return
No
scrollBy() Excellent

Bottom line: Use scrollBy() for relative nudges, scroll() for absolute jumps, and await the Promise when you need to know if a smooth scroll was interrupted.

Conclusion

Element.scrollBy() scrolls an element by a relative amount from its current position. It supports smooth animation, works on any overflow container, and returns a Promise you can await when building polished scroll UX.

Continue with scroll(), scrollTop, scrollIntoView(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use scrollBy() for relative nudges and carousels
  • Use behavior: "smooth" for animated scrolling
  • Await the Promise to run UI after smooth scroll ends
  • Use negative deltas to scroll up or left
  • Prefer options object syntax for clarity

❌ Don’t

  • Confuse scrollBy() with scroll()
  • Scroll elements without overflow content
  • Assume IE returns a Promise
  • Fire many smooth scrolls without handling interrupted
  • Use absolute coordinates when a delta is simpler

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.scrollBy()

Relative scroll deltas—optional smooth animation and Promise result.

5
Core concepts
📄 02

Delta

relative

Offset
✍️ 03

Flag

interrupted

Promise
04

Baseline

widely available

Status
05

Pair

scroll()

Absolute

❓ Frequently Asked Questions

It scrolls an element by the given amount relative to its current position (MDN). Pass x/y deltas or an options object with top, left, and behavior.
No. MDN marks Element.scrollBy() as Baseline Widely available (across browsers since January 2020). It is not Deprecated, Experimental, or Non-standard.
A Promise that fulfills with an object containing interrupted — true if another programmatic scroll interrupted this one before it finished (MDN).
scrollBy() adds a delta to the current scroll position. scroll() (alias of scrollTo()) jumps to absolute coordinates inside the element.
Optional string: smooth (animated), instant (jump), or auto (uses CSS scroll-behavior). Default is auto (MDN).
Yes. Pass a positive or negative left/x value, e.g. scrollBy(200, 0) or scrollBy({ left: 200, behavior: "smooth" }).
Did you know?

element.scrollBy() adds pixels to the current scroll position. Use scroll() when you need to jump to an absolute coordinate instead.

Next: scrollIntoView()

Continue the Element series with reveal-into-view scrolling.

scrollIntoView() →

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