JavaScript Window scrollBy() Method

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

What You’ll Learn

The scrollBy() method moves the page (or a scrollable element) by a relative pixel offset. This tutorial covers syntax, smooth scrolling, positive and negative values, how it differs from scrollTo(), reading scrollX / scrollY, five examples, and common UI patterns.

01

Syntax

scrollBy(x, y)

02

Relative

From current spot

03

vs scrollTo

Absolute position

04

Smooth

behavior: "smooth"

05

Signs

+/− for direction

06

Elements

el.scrollBy()

Introduction

Long pages need navigation—back-to-top buttons, “next section” controls, carousels, and horizontal galleries. The window.scrollBy() method lets JavaScript nudge the viewport by a number of pixels from wherever the user currently is.

Unlike moveBy() or resizeBy(), scrolling the document is allowed on normal tabs. Users still control the scrollbar and touch gestures; your script adds programmatic movement on top.

Understanding the scrollBy() Method

window.scrollBy(deltaX, deltaY) adds pixels to the current scroll position. Positive deltaX moves content left (you see more to the right); positive deltaY scrolls down. The method returns undefined.

Modern browsers also accept a ScrollToOptions object: scrollBy({ top: 300, left: 0, behavior: "smooth" }). Use top / left instead of positional arguments when you want smooth animation.

💡
Beginner Tip

Think of scrollBy as “scroll from here” and scrollTo as “scroll to coordinate (0, 500).” To jump to a heading, element.scrollIntoView() is often simpler.

📝 Syntax

General forms of Window.scrollBy():

JavaScript
window.scrollBy(deltaX, deltaY);

// smooth relative scroll (ScrollToOptions):
window.scrollBy({ top: 300, behavior: "smooth" });

// shorthand (same in browsers):
scrollBy(0, 200);

Parameters

  • deltaX (positional form) — horizontal pixels to scroll. Positive = right; negative = left.
  • deltaY (positional form) — vertical pixels to scroll. Positive = down; negative = up.
  • options (object form) — { top, left, behavior } where behavior is "auto" or "smooth".

Return value

  • undefined.

Related APIs

  • window.scrollTo(x, y) — scroll to absolute document coordinates.
  • window.scrollX / scrollY — current horizontal and vertical scroll offset.
  • element.scrollIntoView() — scroll so an element is visible.
  • element.scrollBy() — relative scroll inside a scrollable container.
  • window.innerHeight — viewport height; useful for “one screen down” steps.

⚡ Quick Reference

TopicDetail
Scroll down 200pxscrollBy(0, 200)
Scroll up 100pxscrollBy(0, -100)
One viewport downscrollBy(0, innerHeight)
Smooth scrollscrollBy({ top: 300, behavior: "smooth" })
Read positionscrollX, scrollY
Absolute jumpscrollTo(x, y) instead

📋 scrollBy() vs scrollTo() vs scrollIntoView()

All three move the viewport; choose relative steps, absolute coordinates, or element targeting.

scrollBy
scrollBy(0, 200)

+200px down from here

scrollTo
scrollTo(0, 500)

Jump to y = 500

scrollIntoView
el.scrollIntoView()

Show a DOM element

CSS
scroll-behavior

Smooth anchor links

Examples Gallery

Use the Try-it links on pages with enough content to scroll. Each example nudges the viewport by a relative offset.

📚 Getting Started

Move the page down from its current scroll position.

Example 1 — Basic Downward Scroll

Scroll 200 pixels down instantly.

JavaScript
window.scrollBy(0, 200);
console.log("Scrolled down 200px. scrollY =", window.scrollY);
Try It Yourself

How It Works

The first argument is horizontal offset (0 = no horizontal change). The second is vertical. Browsers clamp at document edges—you cannot scroll past the top or bottom.

📈 Practical Patterns

Smooth steps, reverse direction, horizontal galleries, and reading position.

Example 2 — Smooth “Next Section” Scroll

Scroll one viewport height down with smooth animation.

JavaScript
document.getElementById("nextBtn").addEventListener("click", function () {
  window.scrollBy({
    top: window.innerHeight,
    behavior: "smooth"
  });
});
Try It Yourself

How It Works

innerHeight is the visible viewport height. Pairing it with behavior: "smooth" creates a slideshow-like step without knowing absolute pixel coordinates.

Example 3 — Scroll Up with Negative Values

Move back up 150 pixels—useful for “previous” controls.

JavaScript
window.scrollBy(0, -150);
console.log("Scrolled up 150px. scrollY =", window.scrollY);
Try It Yourself

How It Works

Negative vertical values scroll upward. At the top of the page, scrollY stays at 0 even if you call scrollBy(0, -999).

Example 4 — Horizontal Scroll in a Wide Gallery

Scroll a wide container sideways with a positive deltaX.

JavaScript
document.getElementById("gallery").scrollBy({
  left: 250,
  behavior: "smooth"
});
Try It Yourself

How It Works

Apply scrollBy on the scrollable element, not always on window. Horizontal carousels often use overflow-x: auto on a wrapper div.

Example 5 — Log scrollY Before and After

Verify how far the page moved.

JavaScript
const before = window.scrollY;
window.scrollBy(0, 100);
const after = window.scrollY;

console.log(`Before: ${before}px`);
console.log(`After:  ${after}px`);
Try It Yourself

How It Works

scrollY (alias pageYOffset) is the number of pixels the document has been scrolled vertically. With smooth behavior, read position inside a scroll event if you need mid-animation values.

🚀 Common Use Cases

  • Next / Previous section — step through full-page layouts.
  • Back to top (small nudge) — or use scrollTo(0, 0) for instant top.
  • Horizontal carouselscontainer.scrollBy({ left: 200, behavior: "smooth" }).
  • Keyboard-style controls — arrow buttons that scroll fixed pixel steps.
  • Tutorial walkthroughs — reveal content below the fold on demand.
  • Reading position tweaks — fine adjustments after layout changes.

🧠 What Happens When You Call scrollBy()

1

Pass offsets

Script supplies horizontal and vertical pixel deltas (or a ScrollToOptions object).

Args
2

Add to position

Current scrollX/scrollY plus deltas becomes the target (clamped to document bounds).

Relative
3

Animate (optional)

With behavior: "smooth", the browser animates over a short duration.

Smooth
4

Viewport moves

User sees new content; method returns undefined.

📝 Notes

  • Scrolling past document edges is silently clamped—no error is thrown.
  • For anchor links, CSS scroll-behavior: smooth on html can replace JavaScript for simple hash navigation.
  • Detecting “near bottom” for infinite scroll uses the scroll event and scrollY, not repeated scrollBy calls.
  • Respect prefers-reduced-motion—skip smooth scrolling when users request reduced motion.
  • scrollBy on window scrolls the document; on an element it scrolls that element’s overflow area.
  • To scroll to a specific heading, prefer element.scrollIntoView({ behavior: "smooth" }).

Browser Support

Window.scrollBy() is supported in every major browser. The object form with behavior: "smooth" is widely supported in modern browsers.

Universal · Standard API

Window.scrollBy()

Supported in Chrome, Firefox, Safari, Edge, Opera, and mobile browsers. Positional and ScrollToOptions forms both work on the window and on scrollable elements.

100% Universal 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
scrollBy() Universal

Bottom line: Safe to use for programmatic page and container scrolling. Pair with scrollTo() and scrollIntoView() for complete scroll control.

Conclusion

window.scrollBy() moves the viewport by relative pixel offsets. It is one of the most practical window methods for everyday pages—unlike window move or resize APIs, it works on normal tabs.

Use positional arguments for instant steps, ScrollToOptions for smooth animation, and reach for scrollTo() or scrollIntoView() when you need absolute or element-based targets.

💡 Best Practices

✅ Do

  • Use scrollBy for fixed-step next/previous navigation
  • Pass behavior: "smooth" for animated movement
  • Read scrollY when you need the current position
  • Call element.scrollBy for overflow containers
  • Respect prefers-reduced-motion for accessibility

❌ Don’t

  • Spam scrollBy in a tight loop without user intent
  • Confuse scrollBy with scrollTo coordinates
  • Assume smooth scroll works identically in every browser
  • Force scroll when the user prefers reduced motion
  • Use scrollBy alone to jump to a specific DOM node

Key Takeaways

Knowledge Unlocked

Five things to remember about scrollBy()

Your foundation for relative page scrolling in JavaScript.

5
Core concepts
02

Relative

From current spot.

Delta
🎨 03

Smooth

behavior option.

UX
📍 04

scrollY

Read position.

State
🔄 05

vs scrollTo

Absolute jump.

Compare

❓ Frequently Asked Questions

It scrolls the document by a relative offset from the current scroll position. Positive x scrolls right, positive y scrolls down. Negative values scroll left or up.
scrollBy(dx, dy) moves from where you are now. scrollTo(x, y) jumps to absolute coordinates in the document. Use scrollBy for “next section” steps; use scrollTo or scrollIntoView for a known target.
Yes. Pass a ScrollToOptions object: scrollBy({ top: 300, behavior: "smooth" }). Instant scrolling uses behavior: "auto" (the default).
No. It returns undefined. Read window.scrollX and window.scrollY (or scrollLeft/scrollTop on elements) to inspect position after scrolling.
Yes. Unlike moveBy() or resizeBy(), scrolling the document is allowed on normal pages. Users can still scroll manually at any time.
Yes. Any scrollable element has scrollBy() too: document.getElementById("panel").scrollBy(0, 100). The same relative-offset rules apply.
Did you know?

scrollBy() and scrollTo() share the same ScrollToOptions object shape—so behavior: "smooth" works on both, even though one moves relatively and the other absolutely.

Continue to scrollTo()

Learn how to scroll to exact coordinates with the window.scrollTo() method.

scrollTo() 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.

6 people found this page helpful