如何实现网页中的浮动广告
下面是在页面两边显示的竖型广告
var left_img='<a target=_blank href="#"><img src="1.jpg" border=0 title=""></a>';
//右边
var right_img='<a target=_blank href="#"><img src="1.jpg" border=0></a>';
var delta=0.05
var collection;
function floaters(){
this.items=[];
this.addItem=function(id,x,y,content)
{
document.write('<DIV id='+id+' style="Z-INDEX:10;POSITION:absolute;top:150px;width:80px;height:60px;left:'+(typeof(x)=='string'?eval(x):x)+';top:'+(typeof(y)=='string'?eval(y):y)+'">'+content+'</DIV>');
var newItem={};
newItem.object=document.getElementById(id);
newItem.x=x;
newItem.y=y;
this.items[this.items.length]=newItem;
}
this.play=function()
{
collection=this.items
setInterval('play()',10);
}
}
function play()
{
for(var i=0;i<collection.length;i++)
{
var followObj=collection[i].object;
var followObj_x=(typeof(collection[i].x)=='string'?eval(collection[i].x):collection[i].x);
var followObj_y=(typeof(collection[i].y)=='string'?eval(collection[i].y):collection[i].y);
if(followObj.offsetLeft!=
(document.body.scrollLeft+followObj_x)) {
var dx=(document.body.scrollLeft+followObj_x-followObj.offsetLeft)*delta;
dx=(dx>0?1:-1)*Math.ceil(Math.abs(dx));
followObj.style.left=followObj.offsetLeft+dx;
}
if(followObj.offsetTop!=(document.body.scrollTop+followObj_y)) {
var dy=(document.body.scrollTop+followObj_y-followObj.offsetTop)*delta;
dy=(dy>0?1:-1)*Math.ceil(Math.abs(dy));
followObj.style.top=followObj.offsetTop+dy;
}
followObj.style.display='';
}
}
var theFloaters=new floaters();
theFloaters.addItem('followDiv1','document.body.clientWidth-110',245,''+right_img+'');
theFloaters.addItem('followDiv2',11,245,''+left_img+'');
theFloaters.play();
</script>
下面是在页面自由移动的图片广告
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var isNS = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4));
var _all = '';
var _style = '';
var wwidth, wheight;
var ydir = '++';
var xdir = '++';
var id1, id2, id3;
var x = 1;
var y = 1;
var x1, y1;
if(!isNS) {
_all='all.';
_style='.style';
}
function getwindowsize() {
clearTimeout(id1);
clearTimeout(id2);
clearTimeout(id3);
if (isNS) {
wwidth = window.innerWidth - 55;
wheight = window.innerHeight - 50;
} else {
wwidth = document.body.clientWidth - 55;
wheight = document.body.clientHeight - 50;
}
id3 = setTimeout('randomdir()', 20000);
animate();
}
function randomdir() {
if (Math.floor(Math.random()*2)) {
(Math.floor(Math.random()*2)) ? xdir='--': xdir='++';
} else {
(Math.floor(Math.random()*2)) ? ydir='--': ydir='++';
}
id2 = setTimeout('randomdir()', 20000);
}
function animate() {
eval('x'+xdir);
eval('y'+ydir);
if (isNS) {
pic1.moveTo((x+pageXOffset),(y+pageYOffset))
} else {
pic1.pixelLeft = x+document.body.scrollLeft;
pic1.pixelTop = y+document.body.scrollTop;
}
if (isNS) {
if (pic1.top <= 5+pageYOffset) ydir = '++';
if (pic1.top >= wheight+pageYOffset) ydir = '--';
if (pic1.left >= wwidth+pageXOffset) xdir = '--';
if (pic1.left <= 5+pageXOffset) xdir = '++';
} else {
if (pic1.pixelTop <= 5+document.body.scrollTop) ydir = '++';
if (pic1.pixelTop >= wheight+document.body.scrollTop) ydir = '--';
if (pic1.pixelLeft >= wwidth+document.body.scrollLeft) xdir = '--';
if (pic1.pixelLeft <= 5+document.body.scrollLeft) xdir = '++';
}
id1 = setTimeout('animate()', 30);
}
// End -->
</script>
</head>
<body OnLoad="getwindowsize()" OnResize="getwindowsize()">
<div id="pic1" style="position:absolute; visibility:visible; left:0px; top:0px; z-index:-1">
<img src="1.jpg" border="0">
</div>
<script language="javascript">
var pic1=eval('document.'+_all+'pic1'+_style);
</script>