如何使用 JavaScript 移动窗口在计算机屏幕上的位置?

发布于 2024-12-11 05:51:13 字数 69 浏览 0 评论 0原文

我记得看过一个谷歌地图混搭/音乐视频,它在屏幕上创建、调整大小和移动窗口。使用什么 JavaScript 方法来执行此操作?

I remember seeing a google maps mashup / music video that created, resized, and moved windows on the screen. What javascript methods are used to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若水微香 2024-12-18 05:51:13

我不知道这有多可靠,但是要将窗口相对于当前位置移动,您可以这样做:

window.moveBy(250, 250); //moves 250px to the left and 250px down

http://www.w3schools.com/jsref/met_win_moveby.asp

将窗口移动到屏幕的特定部分:

window.moveTo(0, 0); //moves to the top left corner of the screen (primary)

http://www.w3schools.com/jsref/met_win_moveto.asp

由@Dan Herbert提供:

可能应该注意 window.moveTo(0,0) 将移动到
主屏幕的左上角。对于拥有多个
监视器,您还可以将负坐标发送到另一个监视器上的位置
监视器。您可以检查要移动到的第二台显示器
screen.availLeft。如果为负,您可以将窗口移动那么远
到第二个显示器上。

I don't know how reliable this is, but to move the window relative to it's current position you can do this:

window.moveBy(250, 250); //moves 250px to the left and 250px down

http://www.w3schools.com/jsref/met_win_moveby.asp

To move the window to a certain part of the screen:

window.moveTo(0, 0); //moves to the top left corner of the screen (primary)

http://www.w3schools.com/jsref/met_win_moveto.asp

Courtesy of @Dan Herbert:

It should probably be noted that window.moveTo(0,0) will move to the
top left corner of the primary screen. For people with multiple
monitors you can also send negative coordinates to position on another
monitor. You can check for a second monitor to move to with
screen.availLeft. If it's negative, you can move a window that far
onto the second monitor.

萧瑟寒风 2024-12-18 05:51:13

通过快速谷歌搜索:移动窗口

您正在寻找 Window.moveBy 和 window. moveTo

我也记得那个视频,你选择你的家乡什么的?我非常喜欢那个。

From a quick google search: Moving windows

You're looking for Window.moveBy and window.moveTo

I remember that video too, you select your hometown and whatnot? I quite liked that.

傲娇萝莉攻 2024-12-18 05:51:13

您可以通过更改显示对象的上边距和左边距(它们一起构成其左上角的坐标)来移动显示对象的位置。如果您知道目标位置的绝对坐标,则可以更改边距,对象将移动到该位置。

如果您不知道绝对目标位置,但只知道两个相对增量(即,将窗口向上移动 5 个像素,向右移动 10 个像素),您可以读取对象的上边距和左边距,将它们增加适当的距离,然后从中设置边距。

边距是对象样式的一部分,因此您可以这样说:

  theobject.style.left = 10 + 'px'; 
  theobject.style.top  = 40 + 'px';

对于定位的对象。

You move a displayed object's position by changing its top and left margins, which, together, are the coordinates of its top left corner. If you know the absolute coordinates of the target position, you can change the margins and the object will move to that spot.

If you don't know the absolute target position, but only know two relative deltas (i.e., move the window up 5 pixels and right 10 pixels), you can read the object's top and left margins, increment those by the appropriate distances, and set the margins from that.

Margins are part of the style of the object, so you'd say something like:

  theobject.style.left = 10 + 'px'; 
  theobject.style.top  = 40 + 'px';

for a positioned object.

℉服软 2024-12-18 05:51:13

由于您的链接包含锚点“#”,因此当您单击链接时,会将页面移回顶部。尝试将 href 替换为以下内容:

href="javascript:void(0)"

这将阻止 href 中的任何内容执行。

Since your links contain anchors, "#", when you click on the links it will move the page back to the top. Try replacing the href with something like:

href="javascript:void(0)"

This will prevent anything within the href from executing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文