JavaScript HTML DOM Window screenLeft 和 screenTop 属性
-
Window screenLeft 和 screenTop 属性
screenLeft和screenTop属性返回窗口相对于屏幕的x(水平)和y(垂直)坐标。返回新窗口相对于屏幕的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>");
-
浏览器支持
项 IE/Edge Chrome FireFox Safari Opera 属性 screenLeft and screenTop 支持支持不支持支持支持注意:对于Firefox,请使用“ window.screenX ”和“ window.screenY ”(有关跨浏览器的解决方案,请参阅“更多示例”)。 -
语法
返回screenLeft and screenTop属性:window.screenLeftwindow.screenTop -
技术细节
项目 描述 返回值: 一个数字,表示窗口相对于屏幕的水平和垂直距离,以像素为单位 -
更多例子
跨浏览器解决方案(使用IE8及更早版本的screenX和screenY):
尝试一下// 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>");