Screen 对象


Screen对象

Screen对象是用于与用户的显示器交互的Javascript API,它提供了有关用户屏幕和窗口尺寸的信息以及屏幕上可用的颜色深度。

属性

Screen.width

属性Screen.width返回屏幕的宽度(以像素为单位)。

console.log("屏幕宽度:" + screen.width);

Screen.height

属性Screen.height返回屏幕的高度(以像素为单位)。

console.log("屏幕高度:" + screen.height);

Screen.availWidth

属性Screen.availWidth返回可用于显示内容的屏幕宽度(以像素为单位),不包括窗口工具栏和任务栏。

console.log("可用宽度:" + screen.availWidth);

Screen.availHeight

属性Screen.availHeight返回可用于显示内容的屏幕高度(以像素为单位),不包括窗口工具栏和任务栏。

console.log("可用高度:" + screen.availHeight);

Screen.colorDepth

属性Screen.colorDepth返回屏幕的颜色深度(以位/像素为单位)。

console.log("颜色深度:" + screen.colorDepth);

方法

Screen.browse

方法Screen.browse用于打开一个新窗口,它与window.browse相同。

screen.browse("https://www.baidu.com");

Screen.lockOrientation

方法Screen.lockOrientation用于锁定屏幕的方向,即只允许在特定的方向上查看站点。它接受一个参数,可以接受值为“portrait-primary”,“portrait-secondary”,“landscape-primary”或“landscape-secondary”。

screen.lockOrientation("landscape-primary");

Screen.unlockOrientation

方法Screen.unlockOrientation用于解锁屏幕的方向。

screen.unlockOrientation();

事件

onorientationchange

事件onorientationchange在屏幕方向更改时触发。

window.addEventListener("orientationchange", function(){
    console.log("屏幕方向已更改");
});

示例

以下代码演示了如何使用Screen对象来输出屏幕的相应信息。

console.log("屏幕宽度:" + screen.width);
console.log("屏幕高度:" + screen.height);
console.log("可用宽度:" + screen.availWidth);
console.log("可用高度:" + screen.availHeight);
console.log("颜色深度:" + screen.colorDepth);

screen.browse("https://www.baidu.com");

screen.lockOrientation("landscape-primary");

以上代码将首先输出屏幕的相应信息,然后打开一个新窗口并锁定屏幕的方向。