- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title></title>
- <style type="text/css">
- #box{float:left;position:relative;width:295px;height: 1000px;}
- .div1{}
- .div2{position:fixed;_position:absolute;top:3px;z-index:295;}
- </style>
- </head>
- <body>
- <br/>
- <br/>
- <br/>
- <br/>
- <br/>
- <br/>
- <br/>
- <br/>
- <br/>
- <br/>
- <br/>
- <div id="box">
- <div id="float" class="div1">
- //隨滾動移動的部分代碼
- </div>
- </div>
- </body>
- <script type="text/javascript">
- (function(){
- var oDiv=document.getElementById("float");
- var H=0,iE6;
- var Y=oDiv;
- while(Y){H+=Y.offsetTop;YY=Y.offsetParent};
- iE6=window.ActiveXObject&&!window.XMLHttpRequest;
- if(!iE6){
- window.onscroll=function()
- {
- var s=document.body.scrollTop||document.documentElement.scrollTop;
- if(s>H){oDiv.className="div1 div2";if(iE6){oDiv.style.top=(s-H)+"px";}}
- else{oDiv.className="div1";}
- };
- }
- })();
- </script>
- </html>