本帖最后由 zj3701838 于 2009-11-13 20:50:48 编辑

解决方案 »

  1.   

    我知道错在哪里,else运行里面的代码只会运行一次,就是不知怎么改比较好
      

  2.   

    原因很明显啊 当图片运行到宽高都是三百的时候 就会执行   if(x_index==1)if(y_index==1)这两个语句
    但是执行一次之后 left 和top就小于300了 接着执行else操作 这样往复执行 就在下面死掉了   你给下面的 if(x_index==1)if(y_index==1)语句改改 比方说再重新赋初始值 1px 看看效果就明白了
      

  3.   

    试验看看,是不是你要的效果。。 var class=
    {
    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>
      

  4.   

    上面JS有一个错误,小写的class是保留字,改成大写就行了:Class