天天看點

Js解決ie6不支援fixed的方法

<!DOCTYPE HTML>
   <html>
   <head>
     <meta http-equiv="content-type" charset="utf-8" />
     <title>Js解決ie6不支援fixed的方法</title>
   </head>
   <style type="text/css">
   *{margin:0;padding: 0;background-color:#000; }
    
    div#ggLeft{position: fixed;_position:absolute;width: 100px;height: 200px;left: 0;top:200px;background-color: red;}
    div#ggRight{position: fixed;_position:absolute;width: 100px;height: 200px;right: 0;bottom: 0;background-color: red;}

 
   </style>
   <body style="height:4000px;">
 <div id="ggLeft" style="left:0;top:200px;">left</div>
 <div id="ggRight" style="right:0;bottom:0;">right</div>

   <script type="text/javascript">
   function ie6_fixed(id, top){
      if(!!window.ActiveXObject && !window.XMLHttpRequest ){//IE6
        var div = document.getElementById(id);
        div.style.position = "absolute";
        window.attachEvent("onscroll", function(){
          setTimeout(function(){//定時器
            div.style.top = (document.documentElement.scrollTop + top )+"px";
          },80)
        });
      }
    }

ie6_fixed("ggLeft", 200);
ie6_fixed("ggRight", 600);

</script>

   </body>
   </html>