js实现div动态改变大小
2016-02-03 16:54
766 查看
网上看到的相关代码,记下,以备日后使用。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
margin:0;
padding:0;
}
#header
{
height: 70px;
background-color: Blue;
}
#middle
{
height: auto;
background-color: Green;
}
#footer
{
height: 30px;
background-color: Gray;
}
</style>
</head>
<body>
<div>
<div id="header">
</div>
<div id="middle">
</div>
<div id="footer">
</div>
</div>
<script type="text/javascript">
var winWidth = 0;
var winHeight = 0;
function findDimensions() { //函数:获取尺寸
//获取窗口宽度
if (window.innerWidth) {
winWidth = window.innerWidth;
}
else if ((document.body) && (document.body.clientWidth)) {
winWidth = document.body.clientWidth;
}
//获取窗口高度
if (window.innerHeight) {
winHeight = window.innerHeight;
}
else if ((document.body) && (document.body.clientHeight)) {
winHeight = document.body.clientHeight;
}
//通过深入Document内部对body进行检测,获取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
winHeight = document.documentElement.clientHeight-100;
winWidth = document.documentElement.clientWidth;
}
//设置div的具体宽度=窗口的宽度的%
if (document.getElementById("middle")) {
document.getElementById("middle").style.height = winHeight + "px";
}
}
findDimensions();
window.onresize = findDimensions;
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
margin:0;
padding:0;
}
#header
{
height: 70px;
background-color: Blue;
}
#middle
{
height: auto;
background-color: Green;
}
#footer
{
height: 30px;
background-color: Gray;
}
</style>
</head>
<body>
<div>
<div id="header">
</div>
<div id="middle">
</div>
<div id="footer">
</div>
</div>
<script type="text/javascript">
var winWidth = 0;
var winHeight = 0;
function findDimensions() { //函数:获取尺寸
//获取窗口宽度
if (window.innerWidth) {
winWidth = window.innerWidth;
}
else if ((document.body) && (document.body.clientWidth)) {
winWidth = document.body.clientWidth;
}
//获取窗口高度
if (window.innerHeight) {
winHeight = window.innerHeight;
}
else if ((document.body) && (document.body.clientHeight)) {
winHeight = document.body.clientHeight;
}
//通过深入Document内部对body进行检测,获取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
winHeight = document.documentElement.clientHeight-100;
winWidth = document.documentElement.clientWidth;
}
//设置div的具体宽度=窗口的宽度的%
if (document.getElementById("middle")) {
document.getElementById("middle").style.height = winHeight + "px";
}
}
findDimensions();
window.onresize = findDimensions;
</script>
</body>
</html>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解
- call/apply/bind 的理解与实例分享
- 如何创建对象以及jQuery中创建对象的方式
- 数组方法汇总