JavaScript Window scrollTo() Method

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

What You’ll Learn

The scrollTo() method jumps the page to absolute scroll coordinates. This tutorial covers syntax, smooth scrolling, back-to-top buttons, scrolling to elements, how it differs from scrollBy(), five examples, and when to prefer scrollIntoView().

01

Syntax

scrollTo(x, y)

02

Absolute

Exact coordinates

03

vs scrollBy

Relative offset

04

Smooth

behavior: "smooth"

05

Top

scrollTo(0, 0)

06

Elements

offsetTop / intoView

Introduction

Users expect “Back to top” links, table-of-contents jumps, and smooth navigation to page sections. The window.scrollTo() method moves the viewport to specific pixel coordinates in the document—unlike scrollBy(), which moves relative to where you already are.

scrollTo() works on normal browser tabs and is supported everywhere. Pair it with behavior: "smooth" for animated scrolling, or use CSS scroll-behavior: smooth on html for hash links without JavaScript.

Understanding the scrollTo() Method

window.scrollTo(x, y) sets the horizontal and vertical scroll position of the document. After the call, window.scrollX should equal x and window.scrollY should equal y (within document bounds). The method returns undefined.

Modern browsers accept ScrollToOptions: scrollTo({ top: 500, left: 0, behavior: "smooth" }). Use top / left in the object form and x / y in the positional form—they map to the same coordinates.

💡
Beginner Tip

scrollTo(0, 0) is the classic back-to-top. To scroll a heading into view with less math, element.scrollIntoView({ behavior: "smooth" }) is often easier.

📝 Syntax

General forms of Window.scrollTo():

JavaScript
window.scrollTo(x, y);

// smooth scroll to coordinates (ScrollToOptions):
window.scrollTo({ top: 500, left: 0, behavior: "smooth" });

// shorthand (same in browsers):
scrollTo(0, 0);

Parameters

  • x (positional form) — horizontal scroll position in pixels from the document’s left edge.
  • y (positional form) — vertical scroll position in pixels from the document’s top edge.
  • options (object form) — { top, left, behavior } where behavior is "auto" or "smooth".

Return value

  • undefined.

Related APIs

  • window.scrollBy(dx, dy) — scroll relative to current position.
  • element.scrollIntoView() — scroll so an element is visible.
  • window.scrollX / scrollY — read current scroll offset.
  • element.offsetTop — element’s distance from offset parent (useful with scrollTo).
  • element.scrollTo() — set scroll position inside a scrollable container.

⚡ Quick Reference

TopicDetail
Jump to coordinatesscrollTo(x, y)
Back to topscrollTo(0, 0)
Smooth topscrollTo({ top: 0, behavior: "smooth" })
Scroll to elementscrollTo({ top: el.offsetTop, behavior: "smooth" })
Relative nudgescrollBy(dx, dy) instead
Read positionscrollX, scrollY

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

Choose absolute coordinates, relative steps, or element-based scrolling.

scrollTo
scrollTo(0, 500)

Jump to y = 500

scrollBy
scrollBy(0, 200)

+200px from here

scrollIntoView
el.scrollIntoView()

Show element in view

CSS
#anchor link

Native hash navigation

Examples Gallery

Use the Try-it links on tall pages to jump to coordinates or sections. Each example sets an absolute scroll position.

📚 Getting Started

Jump the viewport to fixed document coordinates.

Example 1 — Scroll to Absolute Coordinates

Move the viewport so scrollY becomes 500.

JavaScript
window.scrollTo(0, 500);
console.log("scrollY =", window.scrollY);
Try It Yourself

How It Works

The first argument is horizontal position (0 = left edge). The second is vertical. Values beyond the document are clamped to the maximum scroll range.

📈 Practical Patterns

Back to top, element targets, horizontal jumps, and button handlers.

Example 2 — Smooth Back to Top

Animate scroll to the document origin.

JavaScript
document.getElementById("topBtn").addEventListener("click", function () {
  window.scrollTo({
    top: 0,
    left: 0,
    behavior: "smooth"
  });
});
Try It Yourself

How It Works

This is the most common production use of scrollTo. Show the button only after the user has scrolled down (optional UX polish).

Example 3 — Scroll to a Section by Element

Use offsetTop to compute the target y coordinate.

JavaScript
const section = document.getElementById("pricing");

window.scrollTo({
  top: section.offsetTop,
  behavior: "smooth"
});
Try It Yourself

How It Works

offsetTop is relative to the offset parent. For complex layouts, getBoundingClientRect().top + window.scrollY or scrollIntoView() may be more reliable.

Example 4 — Horizontal Scroll to a Position

Jump sideways in a wide document or scrollable container.

JavaScript
window.scrollTo({
  left: 300,
  top: window.scrollY,
  behavior: "smooth"
});
Try It Yourself

How It Works

Most pages scroll vertically only. Horizontal scrollTo matters for wide layouts, maps, or timelines with overflow-x.

Example 5 — Navigation Link with scrollTo

Wire a table-of-contents button to a section coordinate.

JavaScript
<nav>
  <button type="button" data-target="faq">FAQ</button>
</nav>

<section id="faq" style="margin-top: 120vh">FAQ content</section>

<script>
  document.querySelector("[data-target='faq']").addEventListener("click", function () {
    const el = document.getElementById("faq");
    window.scrollTo({ top: el.offsetTop, behavior: "smooth" });
  });
</script>
Try It Yourself

How It Works

For simple anchor links, <a href="#faq"> with CSS smooth scroll may be enough. JavaScript gives you full control over timing and offset (e.g. fixed header height).

🚀 Common Use Cases

  • Back to topscrollTo({ top: 0, behavior: "smooth" }).
  • Table of contents — jump to section headings on long docs.
  • Onboarding tours — scroll to highlighted UI regions.
  • Form validation — scroll to the first invalid field.
  • Single-page apps — reset scroll on route change with scrollTo(0, 0).
  • Horizontal timelinesscrollTo({ left: x }) on wide content.

🧠 What Happens When You Call scrollTo()

1

Pass coordinates

Script supplies target x and y (or ScrollToOptions).

Args
2

Clamp to bounds

Browser limits values to valid scroll range (0 to max scroll).

Limits
3

Animate (optional)

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

Smooth
4

Viewport at target

scrollX / scrollY match target; method returns undefined.

📝 Notes

  • Fixed headers may cover content scrolled to with offsetTop—subtract header height or use scroll-margin-top in CSS.
  • Checking if ("scrollTo" in window) is unnecessary in modern browsers—the method is universal.
  • Respect prefers-reduced-motion: reduce—use instant scroll when users request reduced motion.
  • scrollTo on window moves the document; on an element it sets that container’s scrollTop / scrollLeft.
  • SPAs often call scrollTo(0, 0) on navigation so new pages start at the top.
  • For hash links (#section), native behavior plus CSS may replace JavaScript entirely.

Browser Support

Window.scrollTo() is supported in every major browser. The ScrollToOptions form with behavior: "smooth" is supported in all modern browsers.

Universal · Standard API

Window.scrollTo()

Supported in Chrome, Firefox, Safari, Edge, Opera, and mobile browsers. Positional and object forms work on window and 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
scrollTo() Universal

Bottom line: Safe to use for back-to-top, section jumps, and SPA scroll restoration. Pair with scrollBy() and scrollIntoView() for complete control.

Conclusion

window.scrollTo() jumps the viewport to absolute document coordinates. It is ideal for back-to-top buttons, section navigation, and resetting scroll position after route changes.

Use ScrollToOptions for smooth animation, prefer scrollIntoView() when targeting elements is enough, and reach for scrollBy() when you need relative steps from the current position.

💡 Best Practices

✅ Do

  • Use scrollTo({ top: 0, behavior: "smooth" }) for back to top
  • Account for fixed header height when scrolling to sections
  • Reset scroll on SPA route changes with scrollTo(0, 0)
  • Respect prefers-reduced-motion
  • Consider scrollIntoView() for simpler element targeting

❌ Don’t

  • Confuse scrollTo coordinates with scrollBy deltas
  • Force smooth scroll when users prefer reduced motion
  • Assume offsetTop works in every nested layout
  • Scroll aggressively without user intent
  • Feature-detect scrollTo in modern browsers unnecessarily

Key Takeaways

Knowledge Unlocked

Five things to remember about scrollTo()

Your foundation for absolute page scrolling in JavaScript.

5
Core concepts
📍 02

Absolute

Exact coords.

Jump
🎨 03

Smooth

behavior option.

UX
04

Top

(0, 0)

Pattern
🔄 05

vs scrollBy

Relative step.

Compare

❓ Frequently Asked Questions

It scrolls the document to absolute coordinates. scrollTo(0, 500) jumps so the top of the viewport is 500 pixels from the top of the document (unless clamped at the bottom).
scrollTo(x, y) sets the scroll position directly. scrollBy(dx, dy) adds to the current position. Use scrollTo for “go to top” or a known coordinate; use scrollBy for “move down one screen from here.”
Pass a ScrollToOptions object: scrollTo({ top: 0, behavior: "smooth" }). The default behavior is "auto" (instant).
No. It returns undefined. Read window.scrollX and window.scrollY after scrolling to verify the new position.
scrollIntoView() is simpler for most cases—it handles nested scroll containers and alignment. scrollTo({ top: el.offsetTop }) works when you need exact pixel control relative to the document.
Yes for the main document—it scrolls to the top-left origin. On a scrollable element, element.scrollTo(0, 0) resets that element’s scroll position.
Did you know?

Adding scroll-behavior: smooth to your html element makes native <a href="#section"> links animate smoothly—often replacing custom scrollTo() code for simple in-page navigation.

Continue to setInterval()

Learn how to run code on a repeating timer with the window.setInterval() method.

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