我想用javascript获取视口宽度。但不是普通的虚拟视口。我需要逻辑硬件视口,在我的情况下,这不是设置视口元标记的选项。
为了解决我的问题:我想在iPhone 5上获得320像素(640硬件像素,像素比例为2),尽管虚拟视口远远超过320像素。
有没有办法做到这一点?
谢谢,
赫尔穆特
最佳答案
我在本文中找到了答案: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];
}
关于javascript - 有没有一种方法可以在没有视口(viewport)元标记的情况下获得以硬件像素为单位的真实视口(viewport)大小?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28463104/