【问题标题】:Getting the users viewport size in ems以 ems 获取用户视口大小
【发布时间】:2012-05-31 13:02:40
【问题描述】:

编辑
以下代码获取 em 的大小,但与我的 css 不对应。

function doOnOrientationChange() {
        // Orientation of device has changed, change the size of map and move the maphelp (if required)
        var eminpx = $("#1em").width();
        var largescreeninpx = eminpx*66.236;
        switch(window.orientation) {
            case -90:
            case 90:
                // Orientation is landscape
                console.log('66.236ems in px: ' + largescreeninpx + '. Screen width: ' + screen.height);
                $("#map").height(screen.width*0.7); // The screen width is the viewport height of the device because we're in landscape orientation
                // Will only work if we can actually get the em value of the width
                if (screen.height < largescreeninpx) {
                     // Small or medium screen, show map help below map
                     console.log('small screen');
                    $("#maphelp").css('margin-top', 0);
                } else {
                    // Larger screen, show map and map help side-by-side
                    $("#maphelp").css('margin-top', -screen.width*0.7);
                }
                break; 
            default:
                // Orientation is portrait
                alert('66.23ems in px: ' + largescreeninpx + '. Screen width: ' + screen.width);
                $("#map").height(screen.height*0.7);
                // Will only work if we can actually get the em value of the width
                if (screen.width < largescreeninpx) {
                     // Small or medium screen, show map help below map
                    $("#maphelp").css('margin-top', 0);
                } else {
                    // Larger screen, show map and map help side-by-side
                    $("#maphelp").css('margin-top', -screen.height*0.7);
                }
                break;
        }
    }

虽然在 CSS 中横屏 iPad 不会触发 @media screen and (min-width: 30em) and (max-width: 63.236em) {...}(意味着它的宽度超过 63.236ems),但在 JS 中它会触发 console.log('small screen'),显示它在获取时屏幕宽度小于 66.236ems通过 JS?

原始问题(几乎已回答) 我正在尝试围绕“The Goldilocks Approach”创建一个网站。电子表格中的一切都很好,但我正在尝试动态设置嵌入地图的高度,使其始终是用户当前视口高度的 90%。然而,地图旁边也可能有一些“地图帮助”,允许浏览器视口足够大(在本例中为 66.23ems。纵向的 iPad 超过 66.23ems)。下面是我的代码,它被注释以显示什么不起作用。

function doOnOrientationChange() {
        // Orientation of device has changed, change the size of map and move the maphelp (if required)
        switch(window.orientation) {
            case -90:
            case 90:
                // Orientation is landscape
                $("#map").height(screen.width*0.9); // The screen width is the viewport height of the device because we're in landscape orientation
                if ($(window).width() > 66.23) { // Need this in ems
                     // Small or medium screen, show map help below map
                    $("#maphelp").css('margin-top', 0);
                } else {
                    // Larger screen, show map and map help side-by-side
                    $("#maphelp").css('margin-top', -screen.width*0.9);
                }
                break; 
            default:
                // Orientation is portrait
                if ($(window).width() < 66.23) { // Need this in ems
                     // Small or medium screen, show map help below map
                    $("#maphelp").css('margin-top', 0);
                } else {
                    // Larger screen, show map and map help side-by-side
                    $("#maphelp").css('margin-top', -screen.height*0.9);
                }
                break;
        }
    }

那么,我的问题是,我怎样才能通过签入 ems 来触发正确的响应?

【问题讨论】:

    标签: javascript jquery css goldilocks-approach


    【解决方案1】:

    按如下方式创建一个跨度:

    <span id="one_em">M</span>
    

    这样的CSS:

    #one_em {
        display:none;
        width: 1em;
    }
    

    然后使用jquery获取#one_em元素的宽度。

    const emWidth = $("#one_em").width();
    

    注意:使用 1em 作为 id 会导致某些浏览器无法正确连接 CSS,因为有效的 HTML 4 id 必须以字母开头。使用M,所以宽度实际上是一米宽。

    【讨论】:

    • 对此要小心,因为em 是级联的一部分,所以如果声明有任何父级的字体大小也在ems 中指定,宽度值输出会有所不同。使用和指定rem 将是更一致的输出,因为rem 引用了文档的根em
    【解决方案2】:

    简单地学习在 em 和像素之间进行转换应该可以让你到达你需要去的地方:

    https://stackoverflow.com/a/1463032/50358 很好地回答了这个问题:What is the relationship between ems and pixels?

    【讨论】:

    • 我用width: 1em;display: hidden; 创建了一个div。这似乎适用于桌面浏览器,但不适用于 iOS。 var em = $("#1em").width(); var maxwidth = em*66.23。有任何想法吗? (我只是得到“解析错误”)
    • 对不起,我自己的愚蠢,试图将代码放在switch 而不是函数的开头!
    • 这两个似乎不一样,根据 CSS,横向的 iPad 宽度超过 66.236ems,而使用 JS 则不是。请参阅 OP 了解更多信息 :)
    猜你喜欢
    • 2016-09-28
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多