您的位置:首页 > Web前端 > CSS

【css】上下2个固定在视图中,中间自适应布局

2013-02-01 16:39 906 查看
有人想要个兼容布局:

View Code

<!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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
<script type="text/javascript">

window.onscroll = window.onresize = window.onload =function (){
var div1 = document.getElementById('div1')
var div2 = document.getElementById('div2')
setScrollSize(div1, div2)

}
function setScrollSize(div1,div2){
var sHeight = document.documentElement.scrollTop || document.body.scrollTop;
var oHeight = document.documentElement.clientHeight || document.body.clientHeight;
oHeight += (sHeight -100);    //这个100是他本身的高度
div1.style.top = sHeight + 'px';
div2.style.top = oHeight + 'px';
}
</script>
<style type="text/css">
html, body{position: relative; height: 100%;}
*{margin: 0; padding:0;}
#div1{position: absolute; top: 0; left: 0; background: red; height: 100px; width: 100%; z-index: 3}
#div2{position: absolute; bottom: 0; left: 0; background: red; height: 100px;width: 100%; z-index: 3}

#div3{position: absolute; top: 0; border: 1px solid #000; height:100%; width: 100%; z-index: 1; }
#div4{position: absolute; height: 100%; background: green; width: 100%; padding: 100px 0; height: 2000px; z-index: 1}
</style>
</head>
<body>

<div id="div1"></div>
<div id="div2"></div>
<div id="div3">
<div id="div4">xdwadawdw</div>
</div>
</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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
<script type="text/javascript">

window.onscroll = window.onresize = window.onload =function (){
var div1 = document.getElementById('div1')
var div2 = document.getElementById('div2')
setScrollSize(div1, div2)

}
function setScrollSize(div1,div2){
var sHeight = document.documentElement.scrollTop || document.body.scrollTop;
var oHeight = document.documentElement.clientHeight || document.body.clientHeight;
oHeight += (sHeight -100); //这个100是他本身的高度
div1.style.top = sHeight + 'px';
div2.style.top = oHeight + 'px';
}
</script>
<style type="text/css">
html, body{position: relative; height: 100%;}
*{margin: 0; padding:0;}
#div1{position: absolute; top: 0; left: 0; background: red; height: 100px; width: 100%; z-index: 3}
#div2{position: absolute; bottom: 0; left: 0; background: red; height: 100px;width: 100%; z-index: 3}

#div3{position: absolute; top: 0; border: 1px solid #000; height:100%; width: 100%; z-index: 1; }
#div4{position: absolute; height: 100%; background: green; width: 100%; padding: 100px 0; height: 2000px; z-index: 1}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3">
<div id="div4">xdwadawdw</div>
</div>

</body>
</html>

window.onscorll滚动的时候不够平滑,可以考虑用缓冲运动。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: