JavaScript HTML DOM Window screenX 和 screenY 属性

  • Window screenX 和 screenY 属性

    screenLeftscreenTop属性返回窗口相对于屏幕的x(水平)和y(垂直)坐标。
    返回新窗口相对于屏幕的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>");
    尝试一下
  • 浏览器支持

    IE/Edge Chrome FireFox Safari Opera
    属性
    screenLeft and screenTop
    9.0+
    支持
    支持
    支持
    支持
    提示:对于IE8及更早版本,您可以使用“window.screenLeft ”和“window.screenTop”(参见“更多示例”)。
  • 语法

    返回screenX and screenY属性:
    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>");
    
    尝试一下
    跨浏览器解决方案(使用screenLeft和screenTop for IE8及更早版本):
    // 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>");
    
    尝试一下