有没有办法在没有视口元标记的硬件像素中获得真实的视口大小?

时间:2015-02-11 19:44:05

标签: javascript hardware device viewport pixel

我想用javascript获取视口宽度。但不是常见的虚拟视口。我需要逻辑硬件视口,在我看来,它不是设置视口元标记的选项。

要清除我的问题:我想在iPhone 5上获得320像素(640像素比率为2的硬件像素),尽管虚拟视口远远超过320像素。

有办法吗?

感谢, 赫尔穆特

1 个答案:

答案 0 :(得分:0)

我在本文中找到了我的答案:http://menacingcloud.com/?c=viewportScale

..并将其分解为真正必不可少的东西..

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

// 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];
}