2017-01-16 57 views
0

这让我发疯。很多时候我都有传送带,而且我需要用JavaScript响应地重置屏幕高度。问题在于,对于移动设备,屏幕高度包括顶部的小URL窗口,因此页面高度由“window.innerHeight”或$(window).height()计算得出。已关闭。有没有解决方法?随时都会发生。计算移动设备上的屏幕高度

回答

0

使用 'clientWidth' 和 'clientHeight' 代替

Determining the dimensions of elements

+0

好吧,这将工作跨浏览器和响应? –

+0

据我所知,所有浏览器都支持这些属性 –

0

这取决于如果缩放由视口或不进行。只需使用普通的js来获得价值。这将大大easyier到CONTROLE:

//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);

享受;)