Window screen.width 属性
实例
获取屏幕的总宽度:
<!DOCTYPE html><html><body><h2>width 属性</h2><p id="demo"><script>let width = screen.width;document.getElementById("demo").innerHTML = width + "px";</script></body></html>
定义与用法
width 属性返回用户屏幕的总宽度。
width 属性返回以像素为单位的宽度。
width 属性是只读的。
备注
使用 height 属性获取用户屏幕的总高度。
语法
screen.width
返回值
| 类型 | 描述 |
| 一个数字 | 用户屏幕的总宽度(以像素为单位) |
更多实例
所有 screen 属性:
<!DOCTYPE html><html><body><h2>Screen 属性</h2><div id="demo"></div><script>let text = "Total width/height: " + screen.width + "*" + screen.height + "<br>" +"Available width/height: " + screen.availWidth + "*" + screen.availHeight + "<br>" +"Color depth: " + screen.colorDepth + "<br>" +"Color resolution: " + screen.pixelDepth;document.getElementById("demo").innerHTML = text;</script></body></html>
浏览器支持
screen.width 所有浏览器都支持:
| 属性 | |||||
|---|---|---|---|---|---|
| screen.width | Yes | Yes | Yes | Yes | Yes |
相关页面
screen.width 属性