2015-02-11 92 views
1

我想用javascript获取视口宽度。但不是普通的虚拟视口。我需要逻辑硬件视口,在我的情况下,它不是设置视口元标记的选项。有没有办法在没有视口元标记的硬件像素中获得真实的视口大小?

为了解决我的问题:我想在iPhone 5上获得320像素(像素比为2的640个硬件像素),尽管虚拟视口大大超过320像素。

有没有办法做到这一点?

感谢, 赫尔穆特

+0

如果您未设置视口元标记,那么'screen.width'将始终为您提供设备中可用的设备像素宽度,并且如果您设置了视口元标记,它仍然会给您完全相同的数字。这是你要求的吗? – istos 2015-02-11 21:42:12

+0

只有screen.width的问题是,它会在桌面浏览器上显示整个屏幕宽度。但我只想获得浏览器宽度。但我只是找到了解决办法..见下面 – user1030151 2015-02-11 21:59:57

+0

哦!我以为你只是在谈论移动设备。 – istos 2015-02-11 22:04:42

回答

0

我发现我的这篇文章中回答:http://menacingcloud.com/?c=viewportScale

..和breaked下来的真正重要的东西..

..所以,这是我的结果:

// cross browser way to get the common viewport width: 
var viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); 

// cross browser way to get the orientation: 
var isLandscape = document.documentElement.clientWidth > document.documentElement.clientHeight; 

// then get the logical screen width if the screen is smaller than the viewport 
// otherwise get the viewport width 
var screenWidth = screen.width < viewportWidth ? 
    Math[isLandscape ? 'max' : 'min'](screen.width, screen.height) : 
    viewportWidth; 

// screen width 
console.log(screenWidth); 

这里是准备使用的功能大家

function getLogicalDeviceDimensions() { 
    // cross browser way to get the common viewport width: 
    var viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); 
    var viewportHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); 

    // cross browser way to get the orientation: 
    var isLandscape = document.documentElement.clientWidth > document.documentElement.clientHeight; 

    // then get the logical screen size if the screen is smaller than the viewport 
    // otherwise get the viewport size 
    var screenWidth = screen.width < viewportWidth ? 
      Math[isLandscape ? 'max' : 'min'](screen.width, screen.height) : 
      viewportWidth; 

    var screenHeight = screen.height < viewportHeight ? 
      Math[isLandscape ? 'min' : 'max'](screen.width, screen.height) : 
      viewportHeight; 

    return [screenWidth, screenHeight]; 
} 
相关问题