Window screen.colorDepth 属性
实例
获取调色板的比特深度:
<!DOCTYPE html><html><body><h2>colorDepth 属性</h2><p id="demo"><script>let depth = screen.colorDepth;document.getElementById("demo").innerHTML = depth + " bits per pixel";</script></body></html>
定义与用法
colorDepth 属性返回屏幕的颜色深度。
colorDepth 属性返回的单位为位/像素。
colorDepth 属性是只读的。
语法
screen.colorDepth
返回值
| 类型 | 描述 |
| 一个数字 | 屏幕调色板的深度,单位为位/像素: 1, 4, 8, 15, 16, 24, 32, 或 48。 |
更多实例
给 8 位屏幕换另一种背景色(避免 8 位屏幕不支持现代颜色):
<!DOCTYPE html><html><body><h2>colorDepth 属性</h2><script>if (screen.colorDepth <= 8)//8 位屏幕的简单蓝色背景色document.body.style.background = "#0000FF"else//适合现代屏幕的花式蓝色背景色document.body.style.background = "#87CEFA"</script></body></html>
所有 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.colorDepth 所有浏览器都支持:
| 属性 | |||||
|---|---|---|---|---|---|
| screen.colorDepth | Yes | Yes | Yes | Yes | Yes |
相关页面
screen.width 属性