如何获取windowWidth、windowHeight、pageWidth、pageHeight、screenWidth、screenHeight、pageX、pageY、screenX、screenY,这些都可以在所有主要浏览器中使用?
当前回答
获取可用屏幕维度的非jQuery方法。window.screen.width/height已经发布,但为了响应Web设计和完整性,我认为值得提及这些属性:
alert(window.screen.availWidth);
alert(window.screen.availHeight);
http://www.quirksmode.org/dom/w3c_cssom.html#t10 :
availWidth和availHeight-屏幕(不包括OS任务栏等)。
其他回答
这就是我如何在React JS项目中获得屏幕宽度的方法:
如果宽度等于1680,则返回570,否则返回200
var screenWidth = window.screen.availWidth;
<Label style={{ width: screenWidth == "1680" ? 570 : 200, color: "transparent" }}>a </Label>
屏幕有效宽度
使用“控制台”或单击“检查”后,检查任何网站当前加载页面的高度和宽度。
步骤1:单击鼠标右键,单击“检查”,然后单击“控制台”
步骤2:确保您的浏览器屏幕不应处于“最大化”模式。如果浏览器屏幕处于“最大化”模式,您需要首先单击最大化按钮(位于右上角或左上角),然后取消最大化。
步骤3:现在,在大于号('>')后面写以下内容,即。
> window.innerWidth
output : your present window width in px (say 749)
> window.innerHeight
output : your present window height in px (say 359)
与屏幕尺寸相关的完整指南
JavaScript
高度:
document.body.clientHeight // Inner height of the HTML document body, including padding
// but not the horizontal scrollbar height, border, or margin
screen.height // Device screen height (i.e. all physically visible stuff)
screen.availHeight // Device screen height minus the operating system taskbar (if present)
window.innerHeight // The current document's viewport height, minus taskbars, etc.
window.outerHeight // Height the current window visibly takes up on screen
// (including taskbars, menus, etc.)
注意:当窗口最大化时,这将等于screen.availHeight
宽度:
document.body.clientWidth // Full width of the HTML page as coded, minus the vertical scroll bar
screen.width // Device screen width (i.e. all physically visible stuff)
screen.availWidth // Device screen width, minus the operating system taskbar (if present)
window.innerWidth // The browser viewport width (including vertical scroll bar, includes padding but not border or margin)
window.outerWidth // The outer window width (including vertical scroll bar,
// toolbars, etc., includes padding and border but not margin)
滑动分页
高度:
$(document).height() // Full height of the HTML page, including content you have to
// scroll to see
$(window).height() // The current document's viewport height, minus taskbars, etc.
$(window).innerHeight() // The current document's viewport height, minus taskbars, etc.
$(window).outerHeight() // The current document's viewport height, minus taskbars, etc.
宽度:
$(document).width() // The browser viewport width, minus the vertical scroll bar
$(window).width() // The browser viewport width (minus the vertical scroll bar)
$(window).innerWidth() // The browser viewport width (minus the vertical scroll bar)
$(window).outerWidth() // The browser viewport width (minus the vertical scroll bar)
参考:https://help.optimizely.com/Build_Campaigns_and_Experiments/Use_screen_measurements_to_design_for_responsive_breakpoints
2020年全年
我很惊讶这个问题有大约10年的时间,而且到目前为止似乎还没有人给出完整的答案(有10个值)。所以我仔细分析了OP问题(尤其是图片),并发表了一些评论
坐标系(0,0)的中心位于视口(浏览器窗口,没有条和主边框)的左上角,轴指向右下(OP图片上标记的内容),因此pageX、pageY、screenX、screenY的值必须为负值(如果页面较小或未滚动,则为零)对于screenHeight/Width,OP希望计算屏幕高度/宽度,包括系统菜单栏(例如,在MacOs中)-这就是为什么我们不使用.avaidWidth/height(不计算)的原因对于windowWidth/Height,OP不想计算滚动条的大小,所以我们使用.clientWidth/Heith在下面的解决方案中,我们将screenY添加到浏览器左上角的位置(window.screenY),即其菜单/表格/url栏的高度)。但如果下载底部栏出现在浏览器中和/或开发人员控制台在页面底部打开,则很难计算该值——在这种情况下,该值将随着下面的解决方案中该栏/控制台高度的大小而增加。可能不可能读取杆/控制台高度值以进行校正(没有一些技巧,如在测量之前要求用户关闭杆/控制台…)pageWidth-在pageWidth小于windowWidth的情况下,我们需要手动计算<body>子元素的大小以获得该值(我们在下面的解决方案中的contentWidth中进行了示例计算-但通常情况下这可能很困难)为了简单起见,我假设<body>margin=0-如果不是,那么在计算pageWidth/Height和pageX/Y时应该考虑这个值
函数大小(){constcontentWidth=[…document.body.childred].reduce((a,el)=>数学最大值(a,el.getBoundingClientRect().right),0)-document.body.getBoundingClientRect().x;返回{windowWidth:document.documentElement.clientWidth,windowHeight:document.documentElement.clientHeight,pageWidth:数学最小值(document.body.scrollWidth,contentWidth),pageHeight:document.body.scrollHeight,screenWidth:window.screen.width,screenHeight:window.screen.height,pageX:document.body.getBoundingClientRect().x,pageY:document.body.getBoundingClientRect().y,screenX:-window.screenX,screenY:-window.screenY-(window.outerHeight window.innerHeight),}}//测试函数show(){console.log(sizes());}正文{边距:0}.box{width:3000px;height:4000px;background:red;}<div class=“box”>警告:stackoverflow片段给出了错误的screenX-Y值,但如果您将此代码直接复制到页面,则值将正确<br><button onclick=“show()”style=“”>CALC</button></div>
我在Mac OS High Sierra上的Chrome 83.0、Safari 13.1、Firefox 77.0和Edge 83.0上测试了它
如果您需要一个真正防弹的文档宽度和高度解决方案(图片中的pageWidth和pageHeight),您可能需要考虑使用我的插件jQuery.documentSize。
它只有一个目的:即使在jQuery和其他方法失败的情况下,也始终返回正确的文档大小。尽管有jQuery的名字,但您不必使用它——它是用普通的Javascript编写的,也可以在没有jQuery的情况下使用。
用法:
var w = $.documentWidth(),
h = $.documentHeight();
用于全局文档。对于其他文档,例如在您有权访问的嵌入式iframe中,将文档作为参数传递:
var w = $.documentWidth( myIframe.contentDocument ),
h = $.documentHeight( myIframe.contentDocument );
更新:现在也更新窗口尺寸
从1.1.0版开始,jQuery.documentSize还处理窗口维度。
这是必要的,因为
$(window).height()在iOS中有缺陷,以至于没有用$(window).width()和$(window).height()在移动设备上不可靠,因为它们无法处理移动缩放的效果。
jQuery.documentSize提供了$.windowWidth()和$.windowHeight()来解决这些问题。有关更多信息,请查看文档。
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 我如何在网站上使用谷歌的Roboto字体?
- 父ng-repeat从子ng-repeat的访问索引
- 如何禁用文本区域大小调整?
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 保留HTML字体大小时,iPhone的方向从纵向改变为横向
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;