


How to create a floating ad using JavaScript to create a floating ad code_javascript skills
May 16, 2016 pm 05:44 PMIf you have a certain foundation in JavaScript, it is relatively easy to create floating ads. Just upload the code:
<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http: //www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" / >
<title>Untitled Document</title>
<style type="text/css">
*{
margin:0;
padding:0 ;
}
#csdn
{
width:800px;
margin:0 auto;
}
#ad
{
position:absolute;
right:0px;
top:30px;
z-index:1;
}
#cl
{
position:absolute;
right:0px;
top:30px;
z-index:2;
}
</style>
<script type="text/javascript">
var advInitTop=30;
var closeInitTop=30;
function inix()
{
advInitTop=document.getElementById("ad").style.pixelTop;
closeInitTop=document.getElementById("cl") .style.pixelTop;
}
function closeAd()
{
document.getElementById("ad").style.display="none";
document.getElementById("cl" ).style.display="none";
}
function move()
{
//document.getElementById("ad").style.pixelTop=document.body.scrollTop advInitTop;
//document.getElementById("cl").style.pixelTop=document.body.scrollTop closeInitTop;
document.getElementById("ad").style.pixelTop=document.documentElement.scrollTop advInitTop;
document.getElementById("cl").style.pixelTop=document.documentElement.scrollTop closeInitTop;
//alert(document.getElementById("ad").style.pixelTop);
window.status=document .documentElement.scrollTop "/" document.getElementById("ad").style.pixelTop;
}
window.onscroll=move;
</script>
</head>
<body>
<div id="cl" onclick="closeAd()">
<img src="images/close.jpg" />
</ div>
<div id="csdn">
<a href="http://www.csdn.net"><img src="images/xiaojie1.jpg" border= "0" /></a>
</div>
<div id="ad">
<a href="http://www.taobao.com "><img src="images/advpic.gif" /></a>
</div>
</body>
</html>
Key points:
Document.documentElement must be used in the above code, do not use the old document.body, otherwise, it will not comply with w3c standards and you will not see the floating effect!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML, CSS and jQuery: Make a button with a floating effect

HTML layout tips: How to use the position attribute for floating element control

Guide to CSS image properties: outline and display

CSS Floats and Clear Floats: Mastering Floats and Clear Floats

Comprehensive list of CSS layout properties: display, position and float

Discuss the reasons why fixed positioning cannot be used in HTML

CSS drop-down menu property optimization tips: position and z-index
