JavaScript Window moveBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.moveBy() moves the current window by a relative amount in pixels (MDN). It returns undefined.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.moveBy. Start with this line.

JavaScript
moveBy(deltaX, deltaY)
CallWhat it does
moveBy(deltaX, deltaY)Primary signature.
window.moveBy(...)Call with the arguments your case needs.

Built into the browser. Call window.moveBy(...) (or the global moveBy(...)) — no import required.

Library declarations
JavaScript
moveBy(deltaX, deltaY)

Parameters

ParameterDescription
deltaXRequired. Horizontal pixels (+ right, − left)
deltaYRequired. Vertical pixels (+ down, − up)

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic Offset

Move right and down with positive deltas (may be blocked on main tabs).

JavaScript
window.moveBy(100, 50);
// +100 right, +50 down
Try It Yourself

How It Works

  1. Pass two numbers. Horizontal first, vertical second.
  2. Relative move. Adds to the current screen position.
  3. Browser may ignore. Common on the user’s main tab.
  4. Return. Always undefined.

Example 2 — Button Nudge

Each click shifts the window a little further.

JavaScript
document.getElementById("btn").onclick = function () {
  window.moveBy(20, 10);
};
Try It Yourself

How It Works

  1. Wire a click. User gesture helps some browser policies.
  2. Small deltas. Easy to see repeated nudges.
  3. Still relative. Each call starts from the new position.
  4. Tip. Prefer a popup for reliable demos.

Example 3 — Move a Popup

Open a popup, then call moveBy on that window reference.

JavaScript
let popup = null;

function openPopup() {
  popup = window.open("", "MoveDemo", "width=320,height=200,left=200,top=120");
}

function movePopup() {
  if (popup && !popup.closed) {
    popup.moveBy(30, 20);
  }
}
Try It Yourself

How It Works

  1. Open with script. Keep the returned Window.
  2. Guard. Skip if missing or already closed.
  3. Call on the popup. popup.moveBy(...), not only window.
  4. Why it works better. Script-opened single windows are less restricted.

Example 4 — Negative Offset

Negative values move left and up (MDN directions).

JavaScript
window.moveBy(-40, -25);
// 40px left, 25px up
Try It Yourself

How It Works

  1. Negative X. Moves left.
  2. Negative Y. Moves up.
  3. Same API. Still relative to the current position.
  4. Compare. Absolute placement uses moveTo.

Example 5 — Direction Pad

Nudge a popup with Up / Down / Left / Right buttons.

JavaScript
// After opening a popup named `popup`:
popup.moveBy(0, -15);  // up
popup.moveBy(0, 15);   // down
popup.moveBy(-15, 0);  // left
popup.moveBy(15, 0);   // right
Try It Yourself

How It Works

  1. One axis at a time. Set the other delta to 0.
  2. Map buttons. Each UI control chooses its (dx, dy).
  3. Keep a reference. Call methods on the popup object.
  4. Teaching tip. Great way to feel relative movement.

Browser Support

Window.moveBy() is marked Baseline Widely available on MDN (since July 2015). Browser Window API — not a Node.js built-in. Actual movement still depends on popup / OS policy.

✓ Baseline · Widely available

moveBy()

Relative window moves across modern browsers (with UX restrictions).

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support*
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: API is universal; prefer script-opened popups. Do not build layout on moveBy().

Frequently Asked Questions

It moves the current window by a relative pixel offset. deltaX is horizontal (positive = right, negative = left). deltaY is vertical (positive = down, negative = up).
moveBy(dx, dy) shifts from the current position. moveTo(x, y) jumps to absolute screen coordinates.
No. Browsers often block moving tabs the user opened directly, or windows with multiple tabs. It works more reliably on popups created with window.open().
No. It returns undefined. Read screenX / screenY if you need the current position.
Not necessarily. MDN notes it might not move the window synchronously, and on some environments (for example Wayland or mobile) it may not move at all.
No. MDN marks Window.moveBy() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard — but browser UX restrictions still apply.

Next: moveTo()

Move a window to absolute screen coordinates.

moveTo() →

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