JavaScript Window moveBy() Method

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

What You’ll Learn

The moveBy() method shifts a browser window by a relative pixel offset. This tutorial covers syntax, positive and negative values, how it differs from moveTo(), popup usage, browser restrictions, five examples, and when not to use it.

01

Syntax

moveBy(x, y)

02

Relative

From current spot

03

vs moveTo

Absolute coords

04

Popups

popup.moveBy()

05

Signs

+/− for direction

06

Limits

Often blocked

Introduction

Every browser window has a position on the screen. The window.moveBy() method nudges that window by a number of pixels—without you needing to know the current coordinates. Pass 100 for x to shift right; pass -50 for y to shift up.

Like close() and focus(), movement APIs are limited on tabs the user opened themselves. Browsers prevent sites from dragging the user’s main window around unexpectedly. Test with popups created via window.open(), especially when the user clicks a button to trigger the move.

Understanding the moveBy() Method

window.moveBy(deltaX, deltaY) adds deltaX to the window’s current horizontal position and deltaY to its vertical position. Both arguments are numbers (pixels). The method returns undefined.

Called on a popup reference, it moves that child window: popupWindow.moveBy(20, 10). This is the pattern you will see in helper-window tutorials and legacy desktop-style web tools.

💡
Beginner Tip

Think of moveBy as “walk from here” and moveTo as “teleport to coordinates.” For layout inside a page, move DOM elements with CSS—not the browser window.

📝 Syntax

General form of Window.moveBy():

JavaScript
window.moveBy(deltaX, deltaY);
// move a popup reference:
popupWindow.moveBy(20, 10);

Parameters

  • deltaX — pixels to move horizontally. Positive moves right; negative moves left.
  • deltaY — pixels to move vertically. Positive moves down; negative moves up.

Return value

  • undefined.

Related APIs

  • window.moveTo(x, y) — set absolute screen position.
  • window.open() — create a popup you may reposition.
  • window.screenX / window.screenY — read current top-left coordinates.
  • window.resizeBy() — change size by a relative delta (also restricted).

⚡ Quick Reference

TopicDetail
Move current windowwindow.moveBy(dx, dy)
Move popuppopupRef.moveBy(dx, dy)
Move right 50pxmoveBy(50, 0)
Move up 30pxmoveBy(0, -30)
Absolute positionmoveTo(x, y) instead
Main user tabOften cannot move

📋 moveBy() vs moveTo()

Both reposition windows; choose relative nudging or absolute placement.

moveBy
moveBy(20, 10)

+20px right, +10px down

moveTo
moveTo(100, 80)

Top-left at (100, 80)

Read position
screenX, screenY

Current coordinates

In-page UI
CSS transform

Move elements, not window

Examples Gallery

Use the Try-it links to nudge windows from button clicks. Popups opened with window.open() are the most reliable way to see moveBy() work.

📚 Getting Started

Shift the window right and down from its current position.

Example 1 — Basic Relative Move

Move 100 pixels right and 50 pixels down.

JavaScript
window.moveBy(100, 50);
console.log("moveBy(100, 50) called — may be blocked on main tabs.");
Try It Yourself

How It Works

The method runs, but the browser decides whether the window may actually move. On a normal tab the request is often ignored.

📈 Practical Patterns

User-triggered moves, popups, reverse direction, and small steps.

Example 2 — Move on Button Click

Tie movement to a user gesture—the pattern browsers prefer.

JavaScript
document.getElementById("moveBtn").addEventListener("click", function () {
  window.moveBy(20, 10);
  console.log("Window nudged by (20, 10).");
});
Try It Yourself

How It Works

Each click adds another relative offset. Small steps feel less jarring than huge jumps if movement is allowed.

Example 3 — Opener Moves a Popup

Store the open() reference and call moveBy on the child window.

JavaScript
let popupWindow = null;

document.getElementById("openBtn").onclick = function () {
  popupWindow = window.open("", "MoveDemo", "width=320,height=200,left=200,top=120");
  popupWindow.document.write("<p>I am a movable popup</p>");
};

document.getElementById("movePopupBtn").onclick = function () {
  if (popupWindow && !popupWindow.closed) {
    popupWindow.moveBy(30, 20);
    console.log("Popup moved by (30, 20).");
  }
};
Try It Yourself

How It Works

Script-opened popups are the practical target for moveBy(). The opener page keeps the reference and nudges the helper window on demand.

Example 4 — Negative Offsets (Left and Up)

Reverse direction with negative numbers.

JavaScript
// Move 40px left and 25px up from current position
window.moveBy(-40, -25);
console.log("moveBy(-40, -25) called");
Try It Yourself

How It Works

Negative deltaX moves left; negative deltaY moves up. Pair with positive offsets to implement simple arrow-key-style controls on a popup.

Example 5 — Direction Pad for a Popup

Four buttons nudge a popup in cardinal directions.

JavaScript
<button type="button" id="openBtn">Open popup</button>
<button type="button" data-dx="0" data-dy="-15">Up</button>
<button type="button" data-dx="0" data-dy="15">Down</button>
<button type="button" data-dx="-15" data-dy="0">Left</button>
<button type="button" data-dx="15" data-dy="0">Right</button>

<script>
  let popup = null;
  document.getElementById("openBtn").onclick = () => {
    popup = window.open("", "Nudge", "width=280,height=160");
  };
  document.querySelectorAll("[data-dx]").forEach((btn) => {
    btn.onclick = () => {
      if (popup && !popup.closed) {
        popup.moveBy(Number(btn.dataset.dx), Number(btn.dataset.dy));
      }
    };
  });
</script>
Try It Yourself

How It Works

Small repeated moveBy calls build a simple repositioning UI. Store deltas in data-* attributes to keep HTML readable for beginners.

🚀 Common Use Cases

  • Popup tool windows — nudge helper panels the user opened from your app.
  • Legacy desktop-style UIs — multi-window tutorials and intranet tools.
  • Step positioning — fine-tune popup placement after open() when moveTo is overkill.
  • Direction controls — arrow buttons that shift a child window incrementally.
  • Learning demos — contrast relative moveBy with absolute moveTo.
  • Not for responsive layout — use CSS media queries instead of moving the browser window by screen width.

🧠 What Happens When You Call moveBy()

1

Pass deltas

Script supplies horizontal and vertical pixel offsets.

Args
2

Policy check

Browser verifies the window may be repositioned.

Security
3

Add to position

Current screenX/screenY plus deltas becomes the new location.

Relative
4

Window shifts (maybe)

If allowed, the window moves on screen; method returns undefined.

📝 Notes

  • Do not auto-move windows on page load—it frustrates users and is usually blocked.
  • Moving off-screen can make a popup hard to find; clamp or reset with moveTo when needed.
  • Fullscreen and maximized windows may ignore or clamp movement.
  • Mobile browsers often lack traditional movable windows; popups may open as tabs.
  • moveBy affects the outer window—not elements inside the page (use CSS for those).
  • Pair with window.open() features string left and top for initial placement.

Browser Support

Window.moveBy() exists in all major browsers, but whether movement succeeds depends on window type and browser policy—not on missing API support.

Universal API · Restricted behavior

Window.moveBy()

The method is available in Chrome, Firefox, Safari, Edge, and Opera. Script-opened popups move most reliably; user-opened main tabs are often protected.

100% API availability
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
moveBy() Universal

Bottom line: Test with popups from window.open() during user clicks. Never rely on moving the user's primary tab.

Conclusion

window.moveBy() shifts a browser window by relative pixel offsets. It complements moveTo() and fits popup workflows more often than everyday page layout.

Use small steps, trigger moves from user actions, and prefer CSS for in-page design. For helper windows, open with window.open() and call popup.moveBy() on the returned reference.

💡 Best Practices

✅ Do

  • Use moveBy on popups you opened with window.open()
  • Trigger moves from button clicks when possible
  • Keep offsets small for predictable nudging
  • Use negative values for left/up movement
  • Learn moveTo() when you need absolute coordinates

❌ Don’t

  • Move the user’s main tab without clear consent
  • Use moveBy for responsive layout by screen width
  • Apply huge offsets that push windows off-screen
  • Confuse window movement with DOM/CSS positioning
  • Auto-move windows on every page load

Key Takeaways

Knowledge Unlocked

Five things to remember about moveBy()

Your foundation for relative window movement in JavaScript.

5
Core concepts
02

Relative

From current spot.

Delta
🚫 03

Restricted

Main tab blocked.

Policy
🔌 04

Popups

popup.moveBy

open()
📍 05

vs moveTo

Absolute coords.

Pair

❓ Frequently Asked Questions

It moves the current window by a relative offset. The first argument is horizontal pixels (positive = right, negative = left); the second is vertical pixels (positive = down, negative = up).
moveBy(x, y) shifts from the current position. moveTo(x, y) jumps to absolute screen coordinates. Use moveBy for nudging; use moveTo when you know the exact top-left position.
No. Modern browsers restrict programmatic window movement for security and UX. moveBy() may be ignored on tabs the user opened directly. It works more reliably on popups your script opened with window.open().
No. It returns undefined. There is no built-in way to read the new coordinates from the return value—use screenX/screenY on the window if needed.
Yes. Keep the reference from window.open() and call popup.moveBy(dx, dy) on that object, often after a user clicks a button on the opener.
No. Responsive design belongs in CSS and DOM layout. moveBy() is for rare popup positioning workflows—not for adjusting layout based on screen width.
Did you know?

Early web apps used moveBy and moveTo together with resizeBy to mimic floating palettes. Modern sites achieve similar UX with in-page panels and CSS—without fighting browser window restrictions.

Continue to moveTo()

Learn how to position a window at exact screen coordinates with window.moveTo().

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