日期:2014-05-16  浏览次数:20456 次

请问如何判断div被滚动到浏览器顶部?
我想弄个div,它离顶部有段距离,当它被滚动到浏览器顶部的时候,它就固定不动。请问该如何判断?

------解决方案--------------------
HTML code

<html>
<head>
  <title></title>
</head>
<body>
  <div style="height:960px;background:#330000;">teatteat</div>
  <div id="t" style="height:24px;background:#000000;color:#ffffff;text-align:center;">teatteat</div>
  <div style="height:960px;background:#333333;">teatteat</div>
<script type="text/javascript">
document.body.onscroll=function(){
  if(document.body.scrollTop>=document.getElementById("t").offsetTop){
    document.getElementById("t").style.position="absolute";
    document.getElementById("t").style.top=document.body.scrollTop;
  } else {
    document.getElementById("t").style.position="static";
    document.getElementById("t").style.top=document.body.scrollTop;
  }
}
 </script>
</body>
</html>