Javascript 广告定位

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title></title>
 <style type="text/css">
  #ad{
   position:absolute;
   top:120px;
   left:50px;
   100px;
   height:200px;
   background-color:#333;
   color:#fff;
  }
 </style>
 
 <script type="text/javascript">
  window.onload=init;
  window.onscroll=go;
  var oldTop;
  
  function init(){
  
   var ad=document.getElementById("ad");
   
   //取对象原始的top值
   if(window.getComputedStyle){
    oldTop= parseInt( window.getComputedStyle(ad,null).top );
   }else{
    //IE
    oldTop= parseInt(ad.currentStyle.top);
   }
  }
  function go(){
   //浏览器滚动的距离
   var top=document.documentElement.scrollTop;
   
   document.getElementById("ad").style.top= parseInt(top)+oldTop +"px";
   
  }
  //关闭广告
  function closeAd(){
   var ad=document.getElementById("ad");
   ad.parentNode.removeChild(ad);
  }
  
 </script>
</head>
<body>
 <div id="ad"><a href="javascript:;" onclick="closeAd()">X</a><a class="close" onclick="this.parentNode.parentNode.removeChild(this.parentNode)">close</a>广告</div>
 <div style="height:5000px;">sss</div>
 
</body>
</html>

原文地址:https://www.cnblogs.com/wicub/p/3123054.html