<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <style type="text/css"> #a1 { height: 150px; width: 1000px; background: #aaa; } #a2 { height: 50px; width: 1000px; background:#ab9; z-index:999; top:0px; } #a3 { height: 1720px; width: 1000px; background:#fb9; } </style> <script type="text/javascript"> function flo(){ var p=document.body.scrollTop; var i=document.getElementById("a2"); document.getElementById("a3").innerHTML=p; if(p>=document.getElementById("a22").offsetTop){ i.style.position="fixed"; }else{ i.style.position="static"; } } setInterval(flo,1); </script> </head> <body> <div id="a1">这是第一个div</div> <div id="a22"></div> <div id="a2">这是须要浮动的div</div> <div id="a3" onMouseOver="flo();">这是用来占位的div </div> </body> </html>
原本能够用absolute定位的,可是由于函数刷新频率问题,推荐条老是会抖动。javascript
还有一个问题就是js怎么控制fixed的top属性呢?依旧没有用jquery。。。。css