//获取页面及屏幕可见区域尺寸 function getPageSize() { var _pw=Math.max(document.body.scrollWidth,document.documentElement.scrollWidth); var _ph=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight); var _sw=(document.documentElement && document.documentElement.clientWidth)? document.documentElement.clientWidth : document.body.clientWidth; var _sh=(document.documentElement && document.documentElement.clientHeight)? document.documentElement.clientHeight : document.body.clientHeight; return {PW:_pw,PH:_ph,SW:_sw,SH:_sh} }
但是执行一次之后 left 和top就小于300了 接着执行else操作 这样往复执行 就在下面死掉了 你给下面的 if(x_index==1)if(y_index==1)语句改改 比方说再重新赋初始值 1px 看看效果就明白了
{
create:function()
{
return function()
{
this.ready.apply(this,arguments);
}
}
}
var adShow=class.create();
adShow.prototype=
{
ready:function(id,step,speed)
{
this.element=document.getElementById(id);
this.pageSize=getPageSize();
this.defaultPos=
{
x:parseInt(this.element.style.left),
y:parseInt(this.element.style.top)
}
this.stepX=step;
this.stepY=step;
this.speed=speed;
this.action();
},
action:function()
{
var _x=parseInt(this.element.style.left);
var _y=parseInt(this.element.style.top);
if(_x<=this.defaultPos.x)
{
this.stepX=Math.abs(this.stepX)
}
if(_y<=this.defaultPos.y)
{
this.stepY=Math.abs(this.stepY)
}
if(_x+this.element.offsetWidth>=this.pageSize.SW)
{
this.stepX=-this.stepX;
}
if(_y+this.element.offsetHeight>=this.pageSize.SH)
{
this.stepY=-this.stepY;
}
this.element.style.left=_x+this.stepX+"px";
this.element.style.top=_y+this.stepY+"px";
var _self=this;
setTimeout(function(){_self.action()},this.speed);
}
}
//获取scrollTop
function getScrollTop()
{
var _scrollPos;
_scrollPos=(typeof window.pageYOffset != 'undefined')? window.pageYOffset :
(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')? document.documentElement.scrollTop :
(typeof document.body != 'undefined')? document.body.scrollTop : 0;
return _scrollPos;
}
//获取页面及屏幕可见区域尺寸
function getPageSize()
{
var _pw=Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
var _ph=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
var _sw=(document.documentElement && document.documentElement.clientWidth)? document.documentElement.clientWidth : document.body.clientWidth;
var _sh=(document.documentElement && document.documentElement.clientHeight)? document.documentElement.clientHeight : document.body.clientHeight;
return {PW:_pw,PH:_ph,SW:_sw,SH:_sh}
}
<div id="aa" style="position:absolute; left:1px; top:1px;width:200px;height:200px;background:#f00;"><img src="http://c.csdn.net/bbs/t/5/i/pic_logo.gif"></div>
<table width="1000" height="800">
<tr>
<td></td>
</tr>
</table>
<script language='javascript'>
new adShow("aa",2,30);
</script>