js漂浮广告代码大全

发布时间:2019-10-09编辑:脚本学堂
本文介绍了js漂浮广告代码的几种实现方法,js漂浮广告实现代码,实际上就是一个来回滚动的小图片,碰到边缘就返回,也是一种广告形式,感兴趣的朋友做个参考。

第一种,js漂浮广告代码,不符合w3c
javascript漂浮广告代码,很不错,代码精简,不过一次只有漂一个,复制就能用了。
 

复制代码 代码示例:
<html>
<head>
<title>漂浮广告--www.jb200.com</title>
<body>
<div id="codefans_net" style="position:absolute">
<!--链接地址--><a href="#" target="_blank">
<!--图片地址--><img src="/images/logo.gif" border="0">
</a>
</div>
<script>
var x = 50,y = 60
var xin = true, yin = true
var step = 1
var delay = 10
var obj=document.getelementbyid("codefans_net")
function float() {
var l=t=0
var r= document.body.clientwidth-obj.offsetwidth
var b = document.body.clientheight-obj.offsetheight
obj.style.left = x + document.body.scrollleft
obj.style.top = y + document.body.scrolltop
x = x + step*(xin?1:-1)
if (x < l) { xin = true; x = l}
if (x > r){ xin = false; x = r}
y = y + step*(yin?1:-1)
if (y < t) { yin = true; y = t }
if (y > b) { yin = false; y = b }
}
var itl= setinterval("float()", delay)
obj.onmouseover=function(){clearinterval(itl)}
obj.onmouseout=function(){itl=setinterval("float()", delay)}
</script>
</body>
</html>

第二种,js漂浮广告代码,不符合标准。
js漂浮广告代码,比较经典的浮动广告,到现还很实用。
 

复制代码 代码示例:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>漂浮广告</title>
</head>
<body>
<div id=img1 style="z-index: 100; left: 2px; width: 59px; position: absolute; top: 43px; height: 61px;
visibility: visible;"><a href="http://www.jb200.com" target="_blank"><img src="/images/logo.gif" width="80" height="80" border="0"></a></div>
<script>
var xpos = 300;
var ypos = 200;
var step = 1;
var delay = 30;
var height = 0;
var hoffset = 0;
var woffset = 0;
var yon = 0;
var xon = 0;
var pause = true;
var interval;
img1.style.top = ypos;
function changepos()
{
width = document.body.clientwidth;
height = document.body.clientheight;
hoffset = img1.offsetheight;
woffset = img1.offsetwidth;
img1.style.left = xpos + document.body.scrollleft;
img1.style.top = ypos + document.body.scrolltop;
if (yon)
{ypos = ypos + step;}
else
{ypos = ypos - step;}
if (ypos < 0)
{yon = 1;ypos = 0;}
if (ypos >= (height - hoffset))
{yon = 0;ypos = (height - hoffset);}
if (xon)
{xpos = xpos + step;}
else
{xpos = xpos - step;}
if (xpos < 0)
{xon = 1;xpos = 0;}
if (xpos >= (width - woffset))
{xon = 0;xpos = (width - woffset); }
}

function start()
{
img1.visibility = "visible";
interval = setinterval('changepos()', delay);
}
function pause_resume()
{
if(pause)
{
clearinterval(interval);
pause = false;}
else
{
interval = setinterval('changepos()',delay);
pause = true;
}
}
start();
</script>
</body>
</html>

以上两个js漂浮广告代码都是不支持标准声明的。

第三种,符合w3c的漂浮广告代码。
 

复制代码 代码示例:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>符合w3c的漂浮广告代码-脚本学堂</title>
</head>
<body>
<script>
document.write ("<div id=img1 style='z-index: 100; left: 2px; width: 252px; position: absolute; top: 43px; height: 172px; ")
document.write (" visibility: visible;'> ")
document.write (" <a href='http://www.jb200.com' target='_blank'> ")
document.write (" <img src='/upload/201204/20120411210123566.gif'style=border:none;/> ")
document.write (" </a></div> ")
var xpos = 300;
var ypos = 200;
var step = 1;
var delay = 8;
var height = 0;
var hoffset = 0;
var woffset = 0;
var yon = 0;
var xon = 0;
var pause = true;
var interval;
img1.style.top = ypos;
function changepos()
{
width = document.documentelement.clientwidth;
height = document.documentelement.clientheight;
hoffset = img1.offsetheight;
woffset = img1.offsetwidth;
img1.style.left = xpos + document.documentelement.scrollleft;
img1.style.top = ypos + document.documentelement.scrolltop;
if (yon)
{ypos = ypos + step;}
else
{ypos = ypos - step;}
if (ypos < 0)
{yon = 1;ypos = 0;}
if (ypos >= (height - hoffset))
{yon = 0;ypos = (height - hoffset);}
if (xon)
{xpos = xpos + step;}
else
{xpos = xpos - step;}
if (xpos < 0)
{xon = 1;xpos = 0;}
if (xpos >= (width - woffset))
{xon = 0;xpos = (width - woffset); }
}
function start()
{
img1.visibility = "visible";
interval = setinterval('changepos()', delay);
}
function pause_resume()
{
if(pause)
{
clearinterval(interval);
pause = false;}
else
{
interval = setinterval('changepos()',delay);
pause = true;
}
}
start();
</script>
</body>
</html>

以上分享了三个js漂浮广告的代码,第三种符合w3c标准,有需要的朋友做个参考。