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

解决方案 »

  1.   

    if(document.getElementById("test")>=document.getElementById("test").offsetTop)
    不知道行不行~~
      

  2.   

    if(document.body.scrollTop>=document.getElementById("test").offsetTop)
      

  3.   


    <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>随便写的,兼容可能有问题~~而且不流畅