控制DIV占满屏幕

时间:2022-01-17 20:16:06

网上找了很多帖子,希望是CSS控制的,但是在bootstrap环境下, 控制方式上有点问题。达不到想要的效果。

项目中需要实现的效果:

DIV区域占满当前窗口的高度。且在ctrl+鼠标滚轮调整窗口大小的时候,依然占满窗口高度。且,不论DIV区域内容到底多少,都是占满窗口高度,内容多的时候,显示垂直滚动条。左右窗口显示的是一个信息树,ztree实现, 右边窗口将用datatable实现表格。这里重点是DIV高度占满窗口的高度。。。

研究了很多方式,最终还是选择了通过js的方式控制DIV占满当前窗口的功能。代码如下(demo.html):

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DIV Height Full</title>
<link href="../../css/bootstrap.min.css" rel="stylesheet">
<script src="../../js/jquery-2.1.1.min.js"></script>
<script src="../../js/bootstrap.min.js"></script> <link href="../../lib/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<link href="height.css" rel="stylesheet">
<script src="../../lib/zTree/js/jquery.ztree.core.js"></script>
<script src="../../lib/zTree/js/jquery.ztree.excheck.js"></script>
<script src="mytree.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-2">
<div class="sub-border">
<div id="treePanel" class="form-group">
<ul id="myTree" class="ztree" style="overflow: hidden;">
</ul>
</div>
</div>
</div>
<div class="col-md-10">
<div class="sub-border">
<div class="form-group">
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p><p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
<p>nihao</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

下面是sub-border及p的样式内容height.css文件:

 .sub-border{
border: 1px solid #408080;
border-radius: 10px;
background-color: #33ff9a;
overflow-y:auto;
overflow-x:hidden;
} p{
margin: 0 100px;
}

最后看看demo.html中的mytee.js的内容:

 (function($){
var setting = {
// check: {
// enable: true,
// chkboxType:{ "Y":'ps', "N":'ps'}
// },
data: {
simpleData: {
enable: true ,
idKey:"id",
pIdKey:"parentId",
rootPId:-1
}
}
};
var zNodes = [{"id" : 0, "name": "信息中心", "parentId": -1},
{"id" : 1, "name": "DIV", "parentId": 0},
{"id" : 2, "name": "CSS", "parentId": 0},
{"id" : 3, "name": "Height", "parentId": 0}];
var mTree;
$(function(){
$.fn.zTree.init($("#myTree"), setting, zNodes);
mTree = $.fn.zTree.getZTreeObj("myTree");
}) $(function(){
var hw = $(window).height();
var hs = window.screen.height;
var hd = document.documentElement.clientHeight;
console.log(hw);
console.log(hs);
console.log(hd);
$(".sub-border").css("height", hw); $(window).resize(function() {
hw = $(window).height();
$(".sub-border").css("height", hw);
});
}) })(jQuery)

js的重点,就是通过css调整DIV的高度。当ctrl+鼠标滚轮调整窗口大小的时候,用jquery的resize事件调整DIV的高度。

最终的效果如下:

控制DIV占满屏幕