Joh*_*don 280
原始答案
是.
window.screen.availHeight
window.screen.availWidth
Run Code Online (Sandbox Code Playgroud)
更新 2017-11-10
来自Tsunamis的评论:
要获得移动设备的原始分辨率,您必须乘以设备像素比率:
window.screen.width * window.devicePixelRatio和window.screen.height * window.devicePixelRatio.这也适用于台式机,其比率为1.
来自Ben的另一个答案是:
在vanilla JavaScript中,这将为您提供可用的宽度/高度:
Run Code Online (Sandbox Code Playgroud)window.screen.availHeight window.screen.availWidth对于绝对宽度/高度,请使用:
Run Code Online (Sandbox Code Playgroud)window.screen.height window.screen.width
小智 49
var width = screen.width;
var height = screen.height;
Run Code Online (Sandbox Code Playgroud)
Ben*_*Ben 34
在vanilla JavaScript中,这将为您提供可用的宽度/高度:
window.screen.availHeight
window.screen.availWidth
Run Code Online (Sandbox Code Playgroud)
对于绝对宽度/高度,请使用:
window.screen.height
window.screen.width
Run Code Online (Sandbox Code Playgroud)
以上两者都可以在没有窗口前缀的情况下编写.
喜欢jQuery?这适用于所有浏览器,但每个浏览器都提供不同的值.
$(window).width()
$(window).height()
Run Code Online (Sandbox Code Playgroud)
小智 19
使用jQuery,您可以:
$(window).width()
$(window).height()
Run Code Online (Sandbox Code Playgroud)
ser*_*er2 18
您还可以获得WINDOW的宽度和高度,避免浏览器工具栏和...(不仅仅是屏幕大小).
为此,请使用:
window.innerWidth 和 window.innerHeightproperties.在w3schools看到它.
在大多数情况下,它将是显示完美居中的浮动模态对话框的最佳方式.它允许您计算窗口上的位置,无论使用浏览器的分辨率方向或窗口大小.
pos*_*abs 13
尝试在移动设备上获取此功能需要更多步骤.screen.availWidth无论设备的方向如何,都保持不变.
这是我的移动解决方案:
function getOrientation(){
return Math.abs(window.orientation) - 90 == 0 ? "landscape" : "portrait";
};
function getMobileWidth(){
return getOrientation() == "landscape" ? screen.availHeight : screen.availWidth;
};
function getMobileHeight(){
return getOrientation() == "landscape" ? screen.availWidth : screen.availHeight;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
337800 次 |
| 最近记录: |