如何使用 javascript 在计算机屏幕上移动窗口的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7841849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I move a window's position on the computer screen with javascript?
提问by Matrym
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?
我记得看到一个谷歌地图混搭/音乐视频在屏幕上创建、调整大小和移动窗口。使用哪些 javascript 方法来做到这一点?
回答by James Johnson
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
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
http://www.w3schools.com/jsref/met_win_moveto.asp
Courtesy of @Dan Herbert:
由@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 withscreen.availLeft
. If it's negative, you can move a window that far onto the second monitor.
应该注意的是
window.moveTo(0,0)
将移动到主屏幕的左上角。对于有多个显示器的人,您还可以将负坐标发送到另一台显示器上的位置。您可以使用 来检查要移动到的第二台显示器screen.availLeft
。如果它是负数,您可以将一个窗口移到第二台显示器上那么远。
回答by Whetstone
From a quick google search: Moving windows
从一个快速的谷歌搜索:移动窗口
You're looking for Window.moveBy and window.moveTo
您正在寻找 Window.moveBy 和 window.moveTo
I remember that video too, you select your hometown and whatnot? I quite liked that.
我也记得那个视频,你选择你的家乡什么的?我很喜欢那个。
回答by Pete Wilson
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.
如果您不知道绝对目标位置,但只知道两个相对增量(即,将窗口向上移动 5 个像素和向右移动 10 个像素),您可以读取对象的顶部和左侧边距,将它们增加适当的距离,然后从中设置边距。
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.
对于定位的对象。
回答by user2414115
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 :
href="javascript:void(0)"
This will prevent anything within the href from executing.
这将阻止执行 href 中的任何内容。