最新的Web开发教程
 

窗口screenLeft和screenTop属性

<窗口对象

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

var myWindow = window.open("", "myWin");
myWindow.document.write("<p>This is 'myWin'");
myWindow.document.write("<br>ScreenLeft: " + myWindow.screenLeft);
myWindow.document.write("<br>ScreenTop: " + myWindow.screenTop + "</p>");
试一试»

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


定义和用法

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


浏览器支持

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

属性
screenLeft 不支持
screenTop 不支持

Note:对于Firefox,用“ window.screenX ”和“ window.screenY(See "More Examples" for a cross-browser solution)


句法

window.screenLeft
window.screenTop

技术细节

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

例子

更多示例

跨浏览器解决方案(using screenX and screenY 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>");
试一试»

<窗口对象