我想弄个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>