【问题标题】:Calculate height of div automatically based on height of viewport and its position根据视口高度及其位置自动计算div的高度
【发布时间】:2015-06-26 11:25:16
【问题描述】:

我希望 div 在其当前位置和页面底部之间填充整个屏幕的高度。底部会显示一个页脚。

目前,我使用 CSS 如下确定 2 列的高度(见下文 - 它在其父 div 中具有绝对位置)。它基本上可以工作,但不考虑信息面板的高度变化,因为它总是减去-20px;如果显示信息面板,则需要减去大约 200 像素;

top: 0px;
position: absolute;
overflow-y: auto;
height: calc(100vh - 20px);

如何确定 div 的高度,使其从 top:0 一直延伸到屏幕底部 - 页脚为 20px?滚动条(如果显示)应始终到达底部。

谢谢!

	
$(function () {
		$("#infotitle").click(function () {
	
			$('#infopanel').toggle();
		});

	});
* { margin:0px; 
	padding:0px
}

body {
        margin: 0;
        padding: 0;
    }


body {
    background-color: #FFFFFF;
    font-family: Verdana, Geneva, sans-serif;
    font-style: normal;
    font-variant: normal;
    font-size: 0.8em;
    overflow: hidden;
    overflow-y: auto;
}

header {
    height: 30px;
    background-color: #222;
    margin: 0;
    padding: 0;
	display:block;
}

header h1 {
    color: #FFFFFF;
    padding-left: 20px;
    line-height: 30px;
}

h1 {
    font-size: 1.3em;
    font-weight:normal;
}

footer {
	height: 30px;
	z-index: 1;
	background-color: #000000;
	height: 30px;
	width: 100%;
	position: absolute;
	bottom: 0px;
	z-index: -1;
}

.filler {
	height: 8em;
	clear: both;
}

.init {
    overflow-y: hidden;
    overflow: hidden;
}

.ym-wrapper {
	width: 1100px;
	margin: 0 auto;
	padding: 10px;
	
}
* { margin:0px; 
	padding:0px
}

body {
        margin: 0;
        padding: 0;
    }


body {
    background-color: #FFFFFF;
    font-family: Verdana, Geneva, sans-serif;
    font-style: normal;
    font-variant: normal;
    font-size: 0.8em;
    overflow: hidden;
    overflow-y: auto;
}

header {
    height: 30px;
    background-color: #222;
    margin: 0;
    padding: 0;
	display:block;
}

header h1 {
    color: #FFFFFF;
    padding-left: 20px;
    line-height: 30px;
}

h1 {
    font-size: 1.3em;
    font-weight:normal;
}

footer {
	height: 30px;
	z-index: 1;
	background-color: #000000;
	height: 30px;
	width: 100%;
	position: absolute;
	bottom: 0px;
	z-index: -1;
}

.filler {
	height: 8em;
	clear: both;
}

.init {
    overflow-y: hidden;
    overflow: hidden;
}

.ym-wrapper {
	width: 1100px;
	margin: 0 auto;
	padding: 10px;
	
}

.ym-grid {
	display: block;
	table-layout: auto;
	width: 100%;
	list-style-type: none;
	padding-left: 0;
	padding-right: 0;
	margin-left: 0;
	margin-right: 0;
}

.ym-960-5 {
	width: 400px;
	background: lightsteelblue;
	margin-right: 0px;
	position: relative;
	z-index: -3;
	height: 70px;
}
.ym-960-5 h3 {
padding: 10px;
margin-right: 2px;
width: 150px;
float: left;
}

.content-left {
	top: 70px;
	position: absolute;
	overflow-y: auto;
	height: calc(100vh - 300px);
	width: 100%;
}

.ym-gl {
	float: left;
	margin: 0;
}

.ym-g960-7 {
	width: 700px;
	position: relative;
	z-index: -3;
}
#infopanel, #infotitle {
    border: 1px solid #46c94b;
    background: rgba(162, 255, 133, 0.42)  50% top repeat-x;
    color: #363636;
    border-radius: 4px;
    overflow: hidden;
    width: 100%;
    display: table;
}



.infotext {
    width: auto;
    display: table-cell;
    height: 100%;
    padding: 10px;
}

#infonext {
    width: 100px;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    padding: 10px;
}

.ym-grid {
	display: block;
	table-layout: auto;
	width: 100%;
	list-style-type: none;
	padding-left: 0;
	padding-right: 0;
	margin-left: 0;
	margin-right: 0;
}

.ym-960-5 {
	width: 400px;
	background: lightsteelblue;
	margin-right: 0px;
	position: relative;
	z-index: -3;
	height: 70px;
}
.ym-960-5 h3 {
padding: 10px;
margin-right: 2px;
width: 150px;
float: left;
}

.content-left {
	top: 70px;
	position: absolute;
	overflow-y: auto;
	height: calc(100vh - 300px);
	width: 100%;
}

.ym-gl {
	float: left;
	margin: 0;
}

.ym-g960-7 {
	width: 700px;
	position: relative;
	z-index: -3;
}
#infopanel, #infotitle {
    border: 1px solid #46c94b;
    background: rgba(162, 255, 133, 0.42)  50% top repeat-x;
    color: #363636;
    border-radius: 4px;
    overflow: hidden;
    width: 100%;
    display: table;
}



.infotext {
    width: auto;
    display: table-cell;
    height: 100%;
    padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>

	<title>Title</title>
	<link rel="stylesheet" href="test.css" type="text/css"/>
	<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
</head>
<body class="init">
	<header>
		<h1>Title</h1>
	</header>

	<div class="content">
		<div class="ym-wrapper">

			<div class="ym-grid">
				<div id="infotitle">
					<div class="infotext">
						<h3>Title</h3>Click to show/hide panel.
					</div>
				</div>
				<div id="infopanel">
					<div class="infotext">
						<p>text</p>
						<p>text</p>
						<p>text</p>
						<p>text</p>
						<p>text</p>
						<p>text</p>
					</div>
				</div>
				<div class="ym-960-5 ym-gl">
			
					<div><h3>text</h3></div>
					<div class="content-left">
				
						<div id="poiList">
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
							<p>text</p>
						
						</div>
						<div class="filler"></div>
					</div>
				</div>
				<div class="ym-g960-7 ym-gl">
					<p>text</p>
					<p>text</p>
					<p>text</p>
					
				</div>
			</div>
		</div>
	</div>
         
	<footer>
		<div id="outer-container">
			<div id="container-background"></div>
			<div id="container">
			</div>
		</div>
	</footer>
</body>
</html>

【问题讨论】:

  • 请提出您的问题 html 看起来如何。您提供的信息越多,我们就越容易为您提供帮助。
  • 你可以使用弹性盒子。或者一张桌子。是的,请发布一些 HTML。
  • 我添加了一个代码sn-p

标签: javascript css dynamic height


【解决方案1】:

jquery中有一个计算高度的函数,你可以像这样使用它

$(document).ready(function(){
   $("div").height();

}); // This function is used for calculating height when not in responsive
$(document).resize(function(){
   $("div").height();
}); // This function is used for calculating responsive designs on resize

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 2017-10-21
    相关资源
    最近更新 更多