如何获得浏览器的正确视口大小

时间:2014-06-25 00:12:17

标签: javascript html css google-chrome

我正在尝试构建一个占用所有视图端口的应用程序。

然而,window.innerHeight不正确,这影响了我的造型;我的身体标签的高度等于window.innerheight,但文档会滚动(请参阅身体检查下方的屏幕截图)。

window.innerHeight
1198
window.outerHeight
893

body's inspection

显然,这些测量结果是错误的,因为window.innerHeight应始终小于window.outerHeight

我正在使用Chrome,我在另一个窗口中使用inspect元素进行了测量。

有关其他信息,我在下面提供了我的代码。

html doc

<!doctype html>
<html lang="en" ng-app="my-app">
<head>
  <meta charset="utf-8">
  <title>my-app</title>
  <link rel="stylesheet" href="css/bootstrap.css"/>
  <link rel="stylesheet" href="css/app.css"/>
  <link rel="stylesheet" href="css/main.css"/>
</head>
<body id="app_body">
  <div id="fb-root"></div>
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  <script src="js/auth.js"></script>

  <div id="menu">
    <ul style="list-style-type: none;">
      <li id="nav_close_menu_icon"><img src="./img/menu_icon.png" onclick="hideMenu()"/></li>
    </ul>
    <div class="clear"></div>
    <ul id = "menu_options">
        <li id="logout_option" onclick="toggleFbAuthorizationStatus(); hideMenu();"><img id="logout_icon" src="./img/logout_icon.png"/>Logout</li>
    </ul>
  </div>

  <ul class="nav">
    <li><img id="nav_icon" src="./img/icon.png"/></li>
    <li id="nav_open_menu_icon"><img src="./img/menu_icon.png" onclick="showMenu()"/></li>
    <li id="nav_create_message_icon"><img onclick="goTo('create_message')" src="./img/create_hangout_icon.png"/></li>
  </ul>

  <div id="template_container" style="height: 100%" ui-view></div>
  <!-- In production use:
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
  -->
  <script src="js/dynamic-ui.js"></script>
  <script src="js/ui.js"></script>
  <script src="js/bootstrap.js"></script>
  <script src="lib/angular/angular.js"></script>
  <script src="lib/angular/angular-route.js"></script>
  <script src="lib/angular-ui-router.js"/>
  <script src="https://cdn.goinstant.net/v1/platform.min.js"></script>
  <script src="https://cdn.goinstant.net/integrations/goangular/latest/goangular.min.js"></script>
  <script src="js/app.js"></script>
  <script src="js/services.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/filters.js"></script>
  <script src="js/directives.js"></script>
</body>
</html>

样式表

html, body {
    margin: 0;
    height: 100%;
}

#app_body {
        background-color: #178DBA;
        background-image: url('/2048.jpg');
    }

    .nav {
        display: block;
        background-color: #363636;
        list-style-type: none;
        height: 6%;
        /*border-bottom: 0.1em solid #CCCCCC;*/
        margin-bottom: 0em;
        padding: 0 0 0em;
        box-shadow: 0px 3px 5px #191919;
    }

    .clear {
        clear: both;
    }

    .center_horizontal {
        margin-right: auto;
        margin-left: auto;
    }

    #menu {
        position: absolute;
        right: 0px;
        z-index: 10;
        height: 100%;
        background-color: #272727;
        display: none;
        box-shadow: -1px 0px 5px #191919;
    }

    #menu_options {
        background-color: #272727;
        list-style-type: none;
        color: #FFFFFF;
        -webkit-user-select: none;
        font-weight: 200;
    }

    #menu_options li {
        height: 56px;
    }

    #logout_icon {
        width: 50px;
        height: 50px;
        margin-right: 5px;
        vertical-align: middle;
    }

    #logout_option {
        display: none;
    }

    .nav > li {
        display: inline;
    }

    #nav_icon {
        margin-left: 2%
    }

    #nav_open_menu_icon {
        float: right;
        margin-right: 3%;
    }

    #nav_close_menu_icon {
        float: right;
    }

    #nav_create_message_icon img {
        display: none;
        margin-right: 5%;
        float: right;
    }

1 个答案:

答案 0 :(得分:1)

您可以使用此功能获取视口大小。在IE4 +,Firefox,Chrome,Safari,Opera上测试过。

var get_viewport_size = function(){
    if(typeof(window.innerWidth) == 'number'){
        my_width = window.innerWidth;
        my_height = window.innerHeight;
    }else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
        my_width = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
        my_width = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }
    return {width: my_width, height: my_height};
};