87,922
社区成员
发帖
与我相关
我的任务
分享
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>title name</title>
<style type="text/css">
body {margin:0px;padding:0px;}
#a{background:#c2d5ed;width:100%;height:80px;}
#b{width:100px;height:200px;background:#ff6699;}
#b.absolute{position:absolute; left:0px; top:80px;}
#b.fixed{position:fixed; left:0px; top:0px;}
#c{margin-left:100px;width:auto;height:2000px;background:#ffff99;}
</style>
</head>
<body>
<div id="a">aaaaaaa</div>
<div id="b" class="absolute">bbbbbbb</div>
<div id="c">ccccccc</div>
<script>
window.onscroll=function(){
document.getElementById('b').className=document.documentElement.scrollTop>80?"fixed":"absolute";
}
</script>
</body>
</html>
<style>
*{margin:0px;padding:0px;}
.a{background:red;width:100%;height:30px;}
.b{width:50px;height:200px;background:#c0c;left:0px;top:30px;position: absolute;}
.c{padding-left:50px;}
.d{width:10px;height:1000px;background:#ddd;}
</style>
<div class="a"></div>
<div class="b" id="b"></div>
<div class="c"><div class="d">aaaaaaaaaaaaaaaaaa</div></div>
<script>window.onscroll=function(){document.getElementById('b').style.top=window.scrollY>30?(window.scrollY+'px'):'30px';}</script>