2017-01-16 51 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);

享受;)