思路: 如果是关闭飘浮元素,那么就请用 document.body.removeChild(div_obj); 将层元素移除!<script>
var delta=0.165
var collection;
function floaters() {
this.items = [];
this.addItem = function(id,x,y,content)
{
document.write(' <DIV id='+id+' style="Z-INDEX: 0; POSITION: absolute; background-color: #FFFFFF; width:100px; height:20px;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()
{
if(screen.width <=800)
{
for(var i=0;i <collection.length;i++)
{
collection[i].object.style.display = 'none';
}
return;
}
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 = '';
}
} function closediv() {
for (var i=0; i<arguments.length; i++)
{
try
{
document.body.removeChild( document.getElementById(arguments[i]) );
}
catch (e)
{
// do nothing..
}
}
}
var theFloaters = new floaters();
theFloaters.addItem('followDiv1','0','0',' <a href="#" target="_blank"> <img src="/wdwj/images/ggtp/dlgg.gif" border="0" width="100" height="100"> </a> <a onclick=closediv("followDiv1")>一键关闭 </a>');
theFloaters.addItem('followDiv2','document.body.clientWidth-100',0,' <DIV id=topmenu1 align=center> <a href="#" target="_blank"> <img src="/wdwj/images/ggtp/dlgg.gif" border="0" width="100" height="100"> </a> </DIV>');
theFloaters.addItem('followDiv3','0','document.body.clientHeight-150',' <DIV id=topmenu2 align=center> <a href="http://www.abc.cn" target="_blank"> <img src="/wdwj/images/ggtp/cpwgood.gif" border="0" width="200" height="150"> </a> </DIV>'); theFloaters.addItem('followDiv7','document.body.clientWidth-200','document.body.clientHeight-150',' <DIV id=topmenu3 align=center> <a href="http://www.abc.cn" target="_blank"> <img src="/wdwj/images/ggtp/366555.gif" border="0" width="200" height="150"> </a> </DIV>');
theFloaters.play();
</script>
代码说明:
其中做了“红色字体”标记的是新添加的代码,功能就是为了删除指定的层元素,如果你要删除多个层元素,那么你可以在调用 closediv() 的时候,传递多个参数。
var delta=0.165
var collection;
function floaters() {
this.items = [];
this.addItem = function(id,x,y,content)
{
document.write(' <DIV id='+id+' style="Z-INDEX: 0; POSITION: absolute; background-color: #FFFFFF; width:100px; height:20px;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()
{
if(screen.width <=800)
{
for(var i=0;i <collection.length;i++)
{
collection[i].object.style.display = 'none';
}
return;
}
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 = '';
}
} function closediv() {
for (var i=0; i<arguments.length; i++)
{
try
{
document.body.removeChild( document.getElementById(arguments[i]) );
}
catch (e)
{
// do nothing..
}
}
}
var theFloaters = new floaters();
theFloaters.addItem('followDiv1','0','0',' <a href="#" target="_blank"> <img src="/wdwj/images/ggtp/dlgg.gif" border="0" width="100" height="100"> </a> <a onclick=closediv("followDiv1")>一键关闭 </a>');
theFloaters.addItem('followDiv2','document.body.clientWidth-100',0,' <DIV id=topmenu1 align=center> <a href="#" target="_blank"> <img src="/wdwj/images/ggtp/dlgg.gif" border="0" width="100" height="100"> </a> </DIV>');
theFloaters.addItem('followDiv3','0','document.body.clientHeight-150',' <DIV id=topmenu2 align=center> <a href="http://www.abc.cn" target="_blank"> <img src="/wdwj/images/ggtp/cpwgood.gif" border="0" width="200" height="150"> </a> </DIV>'); theFloaters.addItem('followDiv7','document.body.clientWidth-200','document.body.clientHeight-150',' <DIV id=topmenu3 align=center> <a href="http://www.abc.cn" target="_blank"> <img src="/wdwj/images/ggtp/366555.gif" border="0" width="200" height="150"> </a> </DIV>');
theFloaters.play();
</script>
代码说明:
其中做了“红色字体”标记的是新添加的代码,功能就是为了删除指定的层元素,如果你要删除多个层元素,那么你可以在调用 closediv() 的时候,传递多个参数。
<script>
var delta=0.165
var collection;
function floaters() {
this.items = [];
this.addItem = function(id,x,y,content)
{
document.write(' <DIV id='+id+' style="Z-INDEX: 0; POSITION: absolute; background-color: #FFFFFF; width:100px; height:20px;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()
{
if(screen.width <=800)
{
for(var i=0;i <collection.length;i++)
{
collection[i].object.style.display = 'none';
}
return;
}
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 = '';
}
} function closediv() {
for (var i=0; i<arguments.length; i++)
{
try
{
document.body.removeChild( document.getElementById(arguments[i]) );
}
catch (e)
{
// do nothing..
}
}
}
var theFloaters = new floaters();
theFloaters.addItem('followDiv1','0','0',' <a href="#" target="_blank"> <img src="/wdwj/images/ggtp/dlgg.gif" border="0" width="100" height="100"> </a> <a onclick=closediv("followDiv1")>一键关闭 </a>');
theFloaters.addItem('followDiv2','document.body.clientWidth-100',0,' <DIV id=topmenu1 align=center> <a href="#" target="_blank"> <img src="/wdwj/images/ggtp/dlgg.gif" border="0" width="100" height="100"> </a> </DIV>');
theFloaters.addItem('followDiv3','0','document.body.clientHeight-150',' <DIV id=topmenu2 align=center> <a href="http://www.abc.cn" target="_blank"> <img src="/wdwj/images/ggtp/cpwgood.gif" border="0" width="200" height="150"> </a> </DIV>'); theFloaters.addItem('followDiv7','document.body.clientWidth-200','document.body.clientHeight-150',' <DIV id=topmenu3 align=center> <a href="http://www.abc.cn" target="_blank"> <img src="/wdwj/images/ggtp/366555.gif" border="0" width="200" height="150"> </a> </DIV>');
theFloaters.play();
</script>
<a href=onclick=closediv("followDiv1")>一键关闭 </a>好像不行。