首页 > 代码库 > 距离顶部估计像素,显示div!

距离顶部估计像素,显示div!

<html>
<head>
<title>slide</title>
<style type="text/css">
#example1 {width:690px;height:3000px;margin:0 auto;background:#ccc;margin-top:200px;}
#example2 {width:690px;height:2000px;margin:0 auto;background:#eee;}
#navcaidan {width:690px;height:50px;background:black;color:#FFFFFF;margin:0 auto;position:fixed;top:0;left:50%;margin-left:-345px;display:none}
</style>
</head>
<body>
<div id=‘example1‘><></a></div>
<div id=‘example2‘>232323</div>
<div id=‘navcaidan‘>232323</div>
</body>
<script type="text/javascript">
var divMenu = document.getElementById("example1");
var navcaidan = document.getElementById("navcaidan");
function getPageTop(obj){
var ju=0;
ju=ju+obj.offsetTop;
var p=obj.offsetParent;
while(p){
ju=ju+p.offsetTop;
p=p.offsetParent;
}
return ju;
}
var tp=getPageTop(divMenu);
window.onscroll=function(){
var s=document.documentElement.scrollTop||document.body.scrollTop;
if(s>tp){
navcaidan.style.display = "block";
}else{
navcaidan.style.display = "none";
}
}
</script>
</html>