<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#ad{
position:absolute;
top:120px;
left:50px;
width:100px;
height:200px;
background-color:#333;
color:#fff;
}
</style>
<script type="text/javascript">
window.οnlοad=init;
window.οnscrοll=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:;" οnclick="closeAd()">X</a><a class="close" οnclick="this.parentNode.parentNode.removeChild(this.parentNode)">close</a>广告</div>
<div style="height:5000px;">sss</div>
</body>
</html>
Javascript 广告定位
最新推荐文章于 2023-10-01 10:59:57 发布