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()
Fundamentals
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.
Concept
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.
Scrolled down 200px. scrollY = 200
(value depends on starting position)
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.
Click Next → page animates down by one viewport height.
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);
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.
Applications
🚀 Common Use Cases
Next / Previous section — step through full-page layouts.
Back to top (small nudge) — or use scrollTo(0, 0) for instant top.
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.
Important
📝 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" }).
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about scrollBy()
Your foundation for relative page scrolling in JavaScript.
5
Core concepts
📝01
Syntax
scrollBy(x, y)
API
↓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.