是否有一种方法适用于所有浏览器?
当前回答
在JavaScript中,这将为你提供可用的宽度/高度:
window.screen.availHeight
window.screen.availWidth
对于绝对宽度/高度,使用:
window.screen.height
window.screen.width
上面两个代码都可以在没有窗口前缀的情况下编写。
喜欢jQuery吗?这适用于所有浏览器,但每个浏览器给出不同的值。
$(window).width()
$(window).height()
其他回答
原来的答案
Yes.
window.screen.availHeight
window.screen.availWidth
更新2017-11-10
来自海啸的评论:
为了获得移动设备的原生分辨率,你必须与设备像素比相乘:window.screen.width * window.devicePixelRatio和window.screen.height * window.devicePixelRatio。这也适用于桌面,桌面的比率为1。
本的另一个回答是:
在JavaScript中,这将为你提供可用的宽度/高度: window.screen.availHeight window.screen.availWidth 对于绝对宽度/高度,使用: window.screen.height window.screen.width
在JavaScript中,这将为你提供可用的宽度/高度:
window.screen.availHeight
window.screen.availWidth
对于绝对宽度/高度,使用:
window.screen.height
window.screen.width
上面两个代码都可以在没有窗口前缀的情况下编写。
喜欢jQuery吗?这适用于所有浏览器,但每个浏览器给出不同的值。
$(window).width()
$(window).height()
function getScreenWidth()
{
var de = document.body.parentNode;
var db = document.body;
if(window.opera)return db.clientWidth;
if (document.compatMode=='CSS1Compat') return de.clientWidth;
else return db.clientWidth;
}
var width = screen.width;
var height = screen.height;
想要在移动设备上实现这个功能需要更多的步骤。屏幕上。不管设备的方向如何,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;
};