gpt4 book ai didi

javascript - 有没有办法在没有视口(viewport)元标记的情况下获得硬件像素中的真实视口(viewport)大小?

转载 作者:行者123 更新时间:2023-11-28 07:37:11 26 4
gpt4 key购买 nike

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

为了澄清我的问题:我想在 iPhone 5 上获得 320 像素(640 硬件像素,像素比 2),尽管虚拟视口(viewport)远大于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com