javascript - 非全屏时如何获取全屏(最小用户界面) View 的窗口大小?

标签 javascript ios mobile-safari

非全屏时如何获取全屏(最小用户界面) View 的尺寸?

这是什么screen属性报告:

window.screen.height; // 568 (Thats the screen height)
window.screen.availHeight; // 548  (???; Unknown dimension. Note, that is it not reporting the available space as per the spec.)
window.document.documentElement.clientHeight; // 460 (Thats the window size when not in fullscreen.)

全屏时,window.innerHeight是 529,这是我在进入全屏模式之前尝试导出的数字。

图像说明了我所指的屏幕状态:

in fullscreen

在 iOS 8 中,它是通过“触摸向下拖动”手势输入的。

最佳答案

澄清一下,window.screen.availHeight反射(reflect)浏览器窗口(包括所有工具栏)可以具有的最大高度。如果你看下图,你会发现 548 只是整个浏览器的高度,没有 iOS 菜单栏。所以,availHeight属性不应该显示内容的可用空间,而是整个浏览器。它用于桌面浏览器,通过检查 window.outerHeight 来检测浏览器是否最大化。反对 window.screen.availHeight .

iPhone Dimensions

但是关于在进入它之前获得最小的 UI 高度。 spec 中没有标准属性因为它只是 iOS 的行为。 Apple 可以放置一些非标准属性来公开这些信息。但正如我检查过的windowwindow.screen对象,没有任何关系。这是目前的可行性。

作为旁注,了解新视口(viewport)大小与 resize 有什么区别?事件并提前知道可能的规模?在 resize 之前可以完成的工作量事件,应该总是最小的,如果布局发生变化,它应该在调整大小之后发生。因此,提前知道这些数字应该没有多大帮助。

考虑用户打开网页并立即开始滚动的这种情况。如果我们之前知道这些数字,那么对于屏幕的初始渲染我们无能为力,因为它不是最小的 UI。无论应该做什么,都必须在用户开始滚动后立即开始。

现在除此之外,还有另一个看起来像最小 ui 的 resize 事件。在 iPad 上,当你有多个标签时,高度会缩小一点,以显示标签。因此,当用户关闭唯一的其他选项卡时,当前页面会变高一点,并且也会触发调整大小事件。但是这个新的高度与最小 UI 的高度不同。这表明这个最小的用户界面只是高度的另一个提升,而不是类似全屏 API 的模式。

因此,如果页面依赖于高度,则需要记住太多可能的更改。硬编码只是一种短期解决方案,总有一天你将无法测试所有这些设备。

但是,我也应该说调整大小事件有一个缺点,它在进入最小用户界面后触发,这可能为时已晚。所以,当高度开始增长时,没有任何事件可以知道。这就是我相信一些自定义流体 UI 可能需要启动动画来更改大小的地方。在这种情况下,要了解此事件的开始,可以对innerHeight 使用间隔检查,它会立即开始更改。

无论如何,如果有必要的用例在任何滚动之前知道数字,我相信它不能通过 CSSOM 获得,但是。

编辑:

检测最小用户界面是另一回事,仍然不需要知道尺寸。使用触摸事件,您可以检测 View 是否已进入最小 UI。

检查 touchstart 中的内部高度, touchmovetouchend事件可以帮助检测最小的用户界面。天真的方法是在触摸结束时检查innerHeight 是否增加。这适用于大多数情况,但它有一个问题,如果用户在最小 UI 转换的中间停止触摸,我们无法确定我们是否会进入最小 UI。通过 touchmove 检查上次报告的高度可以帮助检查我们是否会恢复到默认 View 。

无论如何,您可以检查下面的代码以了解它是如何工作的。它有错误,可以改进,但你会明白的。我没有把它放在 codepen 上,因为你不能在那里滚动整个页面。

<!doctype html>
<html>
    <head>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no">
        <style>
        div {
            position: fixed;
            left: 10px;
        }
        #dd1 {
            top: 10px;
        }
        #dd2 {
            top:30px;
        }
        #dd3 {
            top: 50px;
        }
        </style>
    <body>
        <div id="dd1">Start: <span id="d1"></span></div>
        <div id="dd2">Move: <span id="d2"></span></div>
        <div id="dd3">End: <span id="d3"></span></div>
        <section style="position:relative;height:3000px;"></section>
        <script>
        var d1 = document.querySelector("#d1");
        var d2 = document.querySelector("#d2");
        var d3 = document.querySelector("#d3");
        var start_height=window.innerHeight;
        var cur_height;
        var end_height;
        var normal_state=true;
        var entered_minimal_ui=false;
        //window.addEventListener("touchstart", start_handler, false);
        window.addEventListener("touchmove", move_handler, false);
        window.addEventListener("touchend", end_handler, false);
        document.ontouchstart = control_touching;
        d1.textContent=start_height;

        function move_handler() {
            if(cur_height>start_height) {
                // we *may* enter the minimal-ui, but it's not final yet
                d2.textContent="I detected minimal-ui faster. (Current: "+cur_height+")";
                normal_state=false;
            }
            cur_height=window.innerHeight;
        }
        function end_handler() {
            end_height=window.innerHeight;

            if(end_height>start_height && end_height>=cur_height) {
                d3.textContent="Hello minimal-ui. (End: "+end_height+")";
                normal_state=false;
                entered_minimal_ui=true;
            }
            else if(!entered_minimal_ui) {
                d3.textContent="We didn't enter minimal-ui. Reverting.";
                normal_state=true;
            }
        }
        function control_touching() {
            document.ontouchstart = function(e){ 
                if(normal_state) {
                    return true;
                }
                else // just for testing
                    e.preventDefault();
            }
        }
        </script>
    </body>
</html>

引用:
  • 插图来自iPhone 5 Display Size and Web Design Tips凯尔·拉森 (Kyle Larson) 的文章。
  • 关于javascript - 非全屏时如何获取全屏(最小用户界面) View 的窗口大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26801943/

    相关文章:

    javascript - 多行标题下划线

    iphone - NSPredicate 检查数组中的所有项目

    javascript - 为什么 'mediaDevices.getUserMedia' 在 ios safari 上比其他浏览器慢?

    css - 在 Safari for Mac 中开发时无法加载 iOS css

    javascript - 将来自 jquery 选择器的值存储在数组或哈希中以供重用

    javascript - DOM 操作 - 使用 javascript 居中 H1 元素

    ios - 在 swift 中使用 valueForKey 时出错

    css - Safari iOS 中出现奇怪的 "filter:grayscale"错误?

    javascript - 如何更改我刚刚单击的按钮的类别而不是具体的?

    ios - 使用 Decodeable swift 4.0 解析数据不读取数据 - Xcode