最新的Web开发教程
 

窗口screenX和screenY属性

<窗口对象

返回新相对于屏幕窗口的X和Y坐标:

var myWindow = window.open("", "myWin");
myWindow.document.write("<p>This is 'myWin'");
myWindow.document.write("<br>ScreenX: " + myWindow.screenX);
myWindow.document.write("<br>ScreenY: " + myWindow.screenY + "</p>");
试一试»

更多"Try it Yourself"下面的例子。


定义和用法

的screenX和screenY属性返回X (horizontal)和y (vertical)相对于屏幕窗口的坐标。


浏览器支持

在表中的数字规定,完全支持该财产浏览器版本。

属性
screenX 9
screenY 9

Tip:对于IE8和更早的版本,你可以使用“ window.screenLeft ”和“ window.screenTop ”,而不是(See "More Examples")


句法

window.screenX
window.screenY

技术细节

返回值: 一个数字,表示在屏幕上相对的窗口的水平和/或垂直距离,以像素为单位

例子

更多示例

打开一个指定的左侧和顶部位置的新窗口,并返回其坐标:

var myWindow = window.open("", "myWin", "left=700, top=350, width=200, height=100");
myWindow.document.write("<p>This is 'myWin'");
myWindow.document.write("<br>ScreenX: " + myWindow.screenX);
myWindow.document.write("<br>ScreenY: " + myWindow.screenY + "</p>");
试一试»

跨浏览器解决方案(using screenLeft and screenTop for IE8 and earlier)

// Open a new window with a specified left and top position
var myWindow = window.open("", "myWin", "left=700, top=350, width=200, height=100");

/*
If the browser does not support screenX and screen Y,
use screenLeft and screenTop instead (and vice versa)
*/
var winLeft = myWindow.screenLeft ? myWindow.screenLeft : myWindow.screenX;
var winTop = myWindow.screenTop ? myWindow.screenTop : myWindow.screenY;

// Write the new window's x and y coordinates relative to the screen
myWindow.document.write("<p>This is 'myWin'");
myWindow.document.write("<br>Horizontal: " + winLeft);
myWindow.document.write("<br>Vertical: " + winTop + "</p>");
试一试»

<窗口对象