Javascript 如何获得浏览器的正确视口大小
我正在尝试构建一个应用程序,它占用了所有的查看端口 但是,Javascript 如何获得浏览器的正确视口大小,javascript,html,css,google-chrome,Javascript,Html,Css,Google Chrome,我正在尝试构建一个应用程序,它占用了所有的查看端口 但是,window.innerHeight不正确,这影响了我的样式;我的body标签的高度等于窗口的innerheight,但文档会滚动(请参见下面的body检查屏幕截图) 显然,这些测量值是错误的,因为window.innerHeight应始终小于window.outerHeight 我使用的是Chrome,我在另一个窗口中使用inspect元素进行测量 为了获得更多信息,我在下面提供了我的代码 html文档 <!doctype ht
window.innerHeight
不正确,这影响了我的样式;我的body标签的高度等于窗口的innerheight
,但文档会滚动(请参见下面的body检查屏幕截图)
显然,这些测量值是错误的,因为window.innerHeight
应始终小于window.outerHeight
我使用的是Chrome,我在另一个窗口中使用inspect元素进行测量
为了获得更多信息,我在下面提供了我的代码
html文档
<!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>
您可以使用此函数获取视口大小。在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};
};
一只已知的虫子?你能显示设置高度的代码吗?看起来innerHeight正在返回计算样式,但无论我做什么,我无法复制此内容。@jme11我提供了css,在其中我设置了html和正文的高度。这并不能解决“外部高度”小于“内部高度”的问题,如中所述question@AlexanderSashaShcherbakov我不确定你是否理解OuterRight和innerHigh之间的区别。快速参考:
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;
}
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};
};