2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Next.jsでWindowの位置を動かす

2
Posted at

今回やること

Next.jsで、window.openを使い新規ウィンドウを作成し、
moveByメソッドでウィンドウを相対的に移動させる。

コード

コード説明

const [childWindow, setChildWindow] = useState<Window | null>(null);

ここで子ウィンドウを入れるためのStateを作っています。

function moveChildWindow(x: number, y: number) {
    childWindow?.moveBy(x, y);
}

ウィンドウを動かす関数はこれです。

<button onClick={() => moveChildWindow(0, -100)}>🔼</button>
<button onClick={() => moveChildWindow(0, 100)}>🔽</button>
<button onClick={() => moveChildWindow(-100, 0)}>◀️</button>
<button onClick={() => moveChildWindow(100, 0)}>▶️</button>

この部分でボタンを作っています。


ウインドウを扱う場合はクライアント側でレンダリングが必要です。
ウィンドウが動かせると色々できそうですね!
最後まで読んでいただきありがとうございます!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?