计算移动设备上的屏幕高度

时间:2017-01-16 22:49:24

标签: javascript android iphone

这让我发疯。我有很多次旋转木马,我需要用JavaScript来重置屏幕高度。问题是,对于移动设备,屏幕高度包括顶部的小URL窗口,因此页面的高度由“window.innerHeight”或$(window).height()计算;已关闭。这有解决方法吗?一直发生。

2 个答案:

答案 0 :(得分:0)

使用' clientWidth'和' clientHeight'代替

Determining the dimensions of elements

答案 1 :(得分:0)

这取决于缩放是否受视口影响。只需使用普通的js来获取值。控制起来会容易得多:

//Screen
var w = screen.width;
var h = screen.height;
console.log('screen = ',w,' & ',h);
//Screen with ratio
var ratio = window.devicePixelRatio || 1;
var w = screen.width * ratio;
var h = screen.height * ratio;
console.log('screen = ',w,' & ',h);

//Document
  //PS document.width & document.height is no longer supported
var w  = window.innerWidth
|| document.documentElement.clientWidth //for IE 8 or lower
|| document.body.clientWidth;  //for IE 8 or lower
var h = window.innerHeight
|| document.documentElement.clientHeight  //for IE 8 or lower
|| document.body.clientHeight;  //for IE 8 or lower
console.log('client = ',w,' & ',h);
//Document offset
var w = document.body.offsetWidth
var h = document.body.offsetHeight
console.log('offset = ',w,' & ',h);
//Document fractional offset
var c = document.body.getBoundingClientRect()
console.log(c);
//Scroll
var x = window.scrollX || window.pageXOffset;
var y = window.scrollY || window.pageYOffset;
console.log('scroll = ',x,' & ',y);

享受;)