我们在打开网页的时候,我们会发现不管是什么网页都会有很多漂浮的广告,有很多人都会在想,这样的效果是怎么实现的呢?今天我们就带大家详细介绍下javascript实现漂浮广告代码的实例总结!
第一种 漂浮广告 不符合w3c
javascript漂浮广告代码,很不错,代码精简,不过一次只有漂一个,复制就能用了。希望站长朋友喜欢。
<html>
<head>
<title>漂浮广告</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漂浮广告代码,比较经典的浮动广告,到现还很实用,如果你是一位站长的话,这种代码是必备的,希望对你有用处。
<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://sc.jb51.net" 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>
上面两个都是不支持标准声明的
第三种 符合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://s.jb51.net' 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>
总结:
本文介绍了javascript实现漂浮广告的几种方法,每个方法使用都不一样,小伙伴们可以根据自己的需求在选择适合自己的,希望对你的工作有所帮助!
相关推荐:
js 居中漂浮广告
js 对联广告、漂浮广告封装类
js随机漂浮广告代码具体实例
以上就是javascript实现漂浮广告代码的实例总结的详细内容。