JavaScript 获取屏幕宽度的实现方法

作者:木棉

时间:2026-10-30

浏览:445

本文介绍如何使用JavaScript获取屏幕分辨率及可用工作区大小。通过调用screen对象的width、height、availWidth和availHeight属性,结合document.write输出结果,可快速获取设备屏幕尺寸信息,适用于响应式布局检测等场景。

文章目录

JavaScript 获取屏幕宽度的实现方法

具体实现代码如下所示:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题文档</title> 
</head> 
 
<body><html> 
<script> 
function a(){ 
document.write( 
"屏幕分辨率为:"+screen.width+"*"+screen.height 
+"<br />"+ 
"屏幕可用大小:"+screen.availWidth+"*"+screen.availHeight 
+"<br />"+ 
"网页可见区域宽:"+document.body.clientWidth 
+"<br />"+ 
"网页可见区域高:"+document.body.clientHeight 
+"<br />"+ 
"网页可见区域宽(包括边线的宽):"+document.body.offsetWidth 
+"<br />"+ 
"网页可见区域高(包括边线的宽):"+document.body.offsetHeight 
+"<br />"+ 
"网页正文全文宽:"+document.body.scrollWidth 
+"<br />"+ 
"网页正文全文高:"+document.body.scrollHeight 
+"<br />"+ 
"网页被卷去的高:"+document.body.scrollTop 
+"<br />"+ 
"网页被卷去的左:"+document.body.scrollLeft 
+"<br />"+ 
"网页正文部分上:"+window.screenTop 
+"<br />"+ 
"网页正文部分左:"+window.screenLeft 
+"<br />"+ 
"屏幕分辨率的高:"+window.screen.height 
+"<br />"+ 
"屏幕分辨率的宽:"+window.screen.width 
+"<br />"+ 
"屏幕可用工作区高度:"+window.screen.availHeight 
+"<br />"+ 
"屏幕可用工作区宽度:"+window.screen.availWidth 
); 
} 
</script> 
<body onload="a()" > 
</body> 
</html> 
</body> 
</html> 

以上就是小编为大家带来的js调用屏幕宽度的简单方法全部内容了,希望大家多多支持本站~

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章