把里面
function move_play()
{
for(var i=0;i <collection.length;i++)
{
var followObj = collection.object;
var followObj_x = followObj.offsetWidth;
var followObj_y = followObj.offsetHeight;
followObj.style.left = collection.xPos + document.documentElement.scrollLeft+'px';
followObj.style.top = collection.yPos + document.documentElement.scrollTop+'px';
if(collection.xon)
{
collection.xPos += step;
}
else
{
collection.xPos -= step;
}
if(collection.yon)
{
collection.yPos += step;
}
else
{
collection.yPos -= step;
}
if(collection.xPos >= (document.documentElement.clientWidth-followObj_x))
{
collection.xon = 0;
collection.xPos = document.documentElement.clientWidth-followObj_x;
}
if(collection.xPos < 0)
{
collection.xon = 1;
collection.xPos = 0;
}
if(collection.yPos >= (document.documentElement.clientHeight-followObj_y))
{
collection.yon = 0;
collection.yPos = document.documentElement.clientHeight-followObj_y;
}
if(collection.yPos < 0)
{
collection.yon = 1;
collection.yPos = 0;
}
}
} 所有的collection变成collection[i]就好了 第一个for语句内部的不变
function move_play()
{
for(var i=0;i <collection.length;i++)
{
var followObj = collection.object;
var followObj_x = followObj.offsetWidth;
var followObj_y = followObj.offsetHeight;
followObj.style.left = collection.xPos + document.documentElement.scrollLeft+'px';
followObj.style.top = collection.yPos + document.documentElement.scrollTop+'px';
if(collection.xon)
{
collection.xPos += step;
}
else
{
collection.xPos -= step;
}
if(collection.yon)
{
collection.yPos += step;
}
else
{
collection.yPos -= step;
}
if(collection.xPos >= (document.documentElement.clientWidth-followObj_x))
{
collection.xon = 0;
collection.xPos = document.documentElement.clientWidth-followObj_x;
}
if(collection.xPos < 0)
{
collection.xon = 1;
collection.xPos = 0;
}
if(collection.yPos >= (document.documentElement.clientHeight-followObj_y))
{
collection.yon = 0;
collection.yPos = document.documentElement.clientHeight-followObj_y;
}
if(collection.yPos < 0)
{
collection.yon = 1;
collection.yPos = 0;
}
}
} 所有的collection变成collection[i]就好了 第一个for语句内部的不变
{
for(var i=0;i<collection.length;i++)
{
var followObj = collection[i].object;
var followObj_x = followObj.offsetWidth;
var followObj_y = followObj.offsetHeight;
followObj.style.left = collection[i].xPos + document.documentElement.scrollLeft+'px';
followObj.style.top = collection[i].yPos + document.documentElement.scrollTop+'px';
if(collection[i].xon)
{
collection[i].xPos += step;
}
else
{
collection[i].xPos -= step;
}
if(collection[i].yon)
{
collection[i].yPos += step;
}
else
{
collection[i].yPos -= step;
}
if(collection[i].xPos >= (document.documentElement.clientWidth-followObj_x))
{
collection[i].xon = 0;
collection[i].xPos = document.documentElement.clientWidth-followObj_x;
}
if(collection[i].xPos < 0)
{
collection[i].xon = 1;
collection[i].xPos = 0;
}
if(collection[i].yPos >= (document.documentElement.clientHeight-followObj_y))
{
collection[i].yon = 0;
collection[i].yPos = document.documentElement.clientHeight-followObj_y;
}
if(collection[i].yPos < 0)
{
collection[i].yon = 1;
collection[i].yPos = 0;
}
}
}
<head>
<title>AD </title>
</head>
<body><script type="text/javascript">
var step = 1; //每次走动的像素
var speed = 30; //图片浮动速度,数值越大,速度越慢(建议设置为30-100之间)
var collection;
function floaters_move()
{
this.items = [];
this.addItem = function(id,x,y,content)
{
document.write(' <div id='+id+' style="z-index:2; position:absolute;">'+content+' </div>');
var newItem = {};
newItem.object = document.getElementById(id);
newItem.xPos = x;
newItem.yPos = y;
newItem.xon = 0;
newItem.yon = 0;
this.items[this.items.length] = newItem;
}
this.play = function()
{
collection = this.items;
setInterval('move_play()',speed);
}
}
function move_play()
{
for(var i=0;i <collection.length;i++)
{
var followObj = collection[i].object;
var followObj_x = followObj.offsetWidth;
var followObj_y = followObj.offsetHeight;
followObj.style.left = collection[i].xPos + document.documentElement.scrollLeft+'px';
followObj.style.top = collection[i].yPos + document.documentElement.scrollTop+'px';
if(collection[i].xon)
{
collection[i].xPos += step;
}
else
{
collection[i].xPos -= step;
}
if(collection[i].yon)
{
collection[i].yPos += step;
}
else
{
collection[i].yPos -= step;
}
if(collection[i].xPos >= (document.documentElement.clientWidth-followObj_x))
{
collection[i].xon = 0;
collection[i].xPos = document.documentElement.clientWidth-followObj_x;
}
if(collection[i].xPos < 0)
{
collection[i].xon = 1;
collection[i].xPos = 0;
}
if(collection[i].yPos >= (document.documentElement.clientHeight-followObj_y))
{
collection[i].yon = 0;
collection[i].yPos = document.documentElement.clientHeight-followObj_y;
}
if(collection[i].yPos < 0)
{
collection[i].yon = 1;
collection[i].yPos = 0;
}
}
}var theFloaters_move = new floaters_move();
//漂浮广告第一个,漂浮广告第二个,第三的类似增加这段代码。当然,只想漂浮一个的话只要这段即可
theFloaters_move.addItem('myad',200,100,' <a href="http://www.google.com" target=_blank> <img src="ad.jpg" width="200px" height="300px" border="0" /> </a>');
theFloaters_move.play();
</script>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</body>
</html>
一:
<img src="ad.jpg" width="200px" height="300px" border="0" /> 这不知你是手误还是你不知怎么写,显然应该改为<img src="ad.jpg" width="200" height="300" border="0" />二:
把<html>改为
<!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">
绝大部分现代出现的JS特效都需要加上这个DOCTYPE申明.三:
把除 "collection.length" 外所有的 "collection." 改成 "collection[i]."
这是最严重的错误,不知这代码是从何处得来的.四:
document.documentElement.scrollLeft 不兼容Google新出的浏览器 Chrome ,
在Chrome应该是document.body.scrollLeft,而这个在IE与FF中的值是0
解决办法是比较这两个值,取最大值
sLeft=Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
document.documentElement.scrollTop 也是同样的道理经修改后的代码如下:(代码我先收藏了)<!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>
<title>AD </title>
</head>
<body>
<div style="height:2000px;">效果测试</div>
<script type="text/javascript">
var step = 1;
var speed = 100;
var collection;
function floaters_move(){
this.items = [];
this.addItem = function(id,x,y,content){
document.write('<div id='+id+' style="z-index:2; position:absolute;">'+content+'</div>');
var newItem = {};
newItem.object = document.getElementById(id);
newItem.xPos = x;
newItem.yPos = y;
newItem.xon = 0;
newItem.yon = 0;
this.items[this.items.length] = newItem;
}
this.play = function(){
collection = this.items;
setInterval('move_play()',speed);
}
}
function move_play(){
for(var i=0;i <collection.length;i++){
var followObj = collection[i].object;
var followObj_x = followObj.offsetWidth;
var followObj_y = followObj.offsetHeight;
var sLeft=Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
var sTop=Math.max(document.documentElement.scrollTop,document.body.scrollTop);
followObj.style.left = (collection[i].xPos + sLeft)+'px';
followObj.style.top = (collection[i].yPos + sTop)+'px';
if(collection[i].xon) collection[i].xPos += step;
else collection[i].xPos -= step;
if(collection[i].yon) collection[i].yPos += step;
else collection[i].yPos -= step;
if(collection[i].xPos >= (document.documentElement.clientWidth-followObj_x)){
collection[i].xon = 0;
collection[i].xPos = document.documentElement.clientWidth-followObj_x;
}
if(collection[i].xPos < 0){
collection[i].xon = 1;
collection[i].xPos = 0;
}
if(collection[i].yPos >= (document.documentElement.clientHeight-followObj_y)){
collection[i].yon = 0;
collection[i].yPos = document.documentElement.clientHeight-followObj_y;
}
if(collection[i].yPos < 0){
collection[i].yon = 1;
collection[i].yPos = 0;
}
}
}var theFloaters_move = new floaters_move();
theFloaters_move.addItem('myad',200,100,'<a href="http://www.google.com" target=_blank><img src="ad.gif" width="200" height="300" border="0" /></a>');
theFloaters_move.play();
</script>
</body>
</html>