Home Web Front-end JS Tutorial How to create a floating ad using JavaScript to create a floating ad code_javascript skills

How to create a floating ad using JavaScript to create a floating ad code_javascript skills

May 16, 2016 pm 05:44 PM
float floating ads

If you have a certain foundation in JavaScript, it is relatively easy to create floating ads. Just upload the code:

Copy the code The code is as follows:

<!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!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

HTML, CSS and jQuery: Make a button with a floating effect HTML, CSS and jQuery: Make a button with a floating effect Oct 24, 2023 pm 12:09 PM

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

Is there any way to clear floats? Is there any way to clear floats? Feb 22, 2024 pm 04:00 PM

Is there any way to clear floats?

HTML layout tips: How to use the position attribute for floating element control HTML layout tips: How to use the position attribute for floating element control Oct 21, 2023 am 09:22 AM

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

Guide to CSS image properties: outline and display Guide to CSS image properties: outline and display Oct 25, 2023 am 08:57 AM

Guide to CSS image properties: outline and display

CSS Floats and Clear Floats: Mastering Floats and Clear Floats CSS Floats and Clear Floats: Mastering Floats and Clear Floats Nov 18, 2023 am 10:56 AM

CSS Floats and Clear Floats: Mastering Floats and Clear Floats

Comprehensive list of CSS layout properties: display, position and float Comprehensive list of CSS layout properties: display, position and float Oct 20, 2023 pm 05:36 PM

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

Discuss the reasons why fixed positioning cannot be used in HTML Discuss the reasons why fixed positioning cannot be used in HTML Jan 20, 2024 am 10:15 AM

Discuss the reasons why fixed positioning cannot be used in HTML

CSS drop-down menu property optimization tips: position and z-index CSS drop-down menu property optimization tips: position and z-index Oct 20, 2023 pm 12:02 PM

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

See all articles