对于全屏画布,如何确定浏览器客户端大小?

问题描述 投票:0回答:3

我已经尝试了各种方法来确定浏览器客户区域的物理像素大小,但我在移动设备上得到了非整数结果。

例如:在带有Chrome的Pixel 2上的window.outerWidth * window.devicePixelRatio产生1081.5,尽管我知道浏览器的客户端区域实际上是1080像素宽。

看到这个的另一种方式是mydevice.ioJS screen.widthJS pixel-ratio相乘。

有没有更好的方法来确定物理客户端的大小?

html5-canvas webgl
3个回答
3
投票

目前没有办法做到这一点,但拟议的ResizeObserver API可能会在未来实现这一目标。

https://github.com/WICG/ResizeObserver/issues/61


-2
投票
<button onclick="myFunction()">Try it</button>

    <p><strong>Note:</strong> The innerWidth and innerHeight properties are not supported in IE8 and earlier.</p>

    <p id="demo"></p>

    <script>
    function myFunction() {
        var w = window.innerWidth;
        var h = window.innerHeight;
        document.getElementById("demo").innerHTML = "Width: " + w + "<br>Height: " + h;
    }
    </script>

-2
投票

如果要排除客户端任务栏,可以使用window.screen.availHeightwindow.screen.availWidth

否则,使用window.screen.heightwindow.screen.width

Source

© www.soinside.com 2019 - 2024. All rights reserved.