this.ID = document.getElementById(arguments[0]);
    if(!this.ID)
    {
        this.ID = -1;
        return;
    }
    this.Direction = this.Width = this.Height = this.DelayTime = this.WaitTime = this.Correct = this.CTL = this.StartID = this.Stop = this.MouseOver = 0;
    this.Step = 1;
    this.Timer = 30;
    this.DirectionArray = {"top":0 , "bottom":1 , "left":2 , "right":3};
    if(typeof arguments[1] == "number")    this.Direction = arguments[1];
    if(typeof arguments[2] == "number")    this.Step = arguments[2];
    if(typeof arguments[3] == "number")    this.Width = arguments[3];
    if(typeof arguments[4] == "number")    this.Height = arguments[4];
    if(typeof arguments[5] == "number")    this.Timer = arguments[5];
    if(typeof arguments[6] == "number")    this.DelayTime = arguments[6];
    if(typeof arguments[7] == "number")    this.WaitTime = arguments[7];
    if(typeof arguments[8] == "number")    this.ScrollStep = arguments[8]   
    this.ID.style.overflow = this.ID.style.overflowX = this.ID.style.overflowY = "hidden";   
    this.ID.noWrap = true;
    this.IsNotOpera = (navigator.userAgent.toLowerCase().indexOf("opera") == -1);
    if(arguments.length >= 7) this.Start();
}Marquee.prototype.Start = function()
{
    if(this.ID == -1)return;
    if(this.WaitTime < 800)this.WaitTime = 800;
    if(this.Timer < 20)this.Timer = 20;
    if(this.Width == 0)this.Width = parseInt(this.ID.style.width);
    if(this.Height == 0)this.Height = parseInt(this.ID.style.height);
    if(typeof this.Direction == "string")this.Direction = this.DirectionArray[this.Direction.toString().toLowerCase()];
    this.HalfWidth = Math.round(this.Width / 2);
    this.BakStep = this.Step;
    this.ID.style.width = this.Width+'px';//必须添加'px',否则IE外的浏览器不支持
    this.ID.style.height = this.Height+'px';//
    if(typeof this.ScrollStep != "number")this.ScrollStep = this.Direction > 1 ? this.Width : this.Height;
    var msobj = this;
    var timer = this.Timer;
    var delaytime = this.DelayTime;
    var waittime = this.WaitTime;
    msobj.StartID = function(){msobj.Scroll()}
    msobj.Continue = function()
                {
                    if(msobj.MouseOver == 1)
                    {
                        setTimeout(msobj.Continue,delaytime);
                    }
                    else
                    {    clearInterval(msobj.TimerID);
                        msobj.CTL = msobj.Stop = 0;
                        msobj.TimerID = setInterval(msobj.StartID,timer);
                    }
                }    msobj.Pause = function()
            {
                msobj.Stop = 1;
                clearInterval(msobj.TimerID);
                setTimeout(msobj.Continue,delaytime);
            }    msobj.Begin = function()
        {
            msobj.ClientScroll = msobj.Direction > 1 ? msobj.ID.scrollWidth : msobj.ID.scrollHeight;
            if((msobj.Direction <= 1 && msobj.ClientScroll <msobj.Height) || (msobj.Direction > 1 && msobj.ClientScroll <msobj.Width))return;
            msobj.ID.innerHTML += msobj.ID.innerHTML;//copy
            msobj.TimerID = setInterval(msobj.StartID,timer);
            if(msobj.ScrollStep < 0)return;
            msobj.ID.onmousemove = function(event)
                        {
                            if(msobj.ScrollStep == 0 && msobj.Direction > 1)
                            {
                                var event = event || window.event;
                                if(window.event)
                                {
                                    if(msobj.IsNotOpera)
                                    {           
                                      msobj.EventLeft = event.srcElement.id == msobj.ID.id ? event.offsetX - msobj.ID.scrollLeft : event.srcElement.offsetLeft - msobj.ID.scrollLeft + event.offsetX;
                                    }
                                    else
                                    {
                                        msobj.ScrollStep = null;                                        return;
                                    }
                                }
                                else
                                {
                                    msobj.EventLeft = event.layerX - msobj.ID.scrollLeft;
                                }
                                msobj.Direction = msobj.EventLeft > msobj.HalfWidth ? 3 : 2;
                                msobj.AbsCenter = Math.abs(msobj.HalfWidth - msobj.EventLeft);
                                msobj.Step = Math.round(msobj.AbsCenter * (msobj.BakStep*2) / msobj.HalfWidth);
                            }
                        }
            msobj.ID.onmouseover = function()
                        {
                            if(msobj.ScrollStep == 0)return;
                            msobj.MouseOver = 1;
                            clearInterval(msobj.TimerID);
                        }
            msobj.ID.onmouseout = function()
                        {
                            if(msobj.ScrollStep == 0)
                            {
                                if(msobj.Step == 0)msobj.Step = 1;
                                return;
                            }
                            msobj.MouseOver = 0;
                            if(msobj.Stop == 0)
                            {
                                clearInterval(msobj.TimerID);
                                msobj.TimerID = setInterval(msobj.StartID,timer);
                            }
                        }
        }
        setTimeout(msobj.Begin,waittime);
}Marquee.prototype.Scroll = function()
{
    switch(this.Direction)
    {
        case 0:
            this.CTL += this.Step;
            if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
            {
                this.ID.scrollTop += this.ScrollStep + this.Step - this.CTL;
                this.Pause();
                return;
            }
            else
            {
                if(this.ID.scrollTop >= this.ClientScroll)
                {
                    this.ID.scrollTop -= this.ClientScroll;
                }
                this.ID.scrollTop += this.Step;
            }
        break;        case 1:
            this.CTL += this.Step;
            if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
            {
                this.ID.scrollTop -= this.ScrollStep + this.Step - this.CTL;
                this.Pause();
                return;
            }
            else
            {
                if(this.ID.scrollTop <= 0)
                {
                    this.ID.scrollTop += this.ClientScroll;
                }
                this.ID.scrollTop -= this.Step;
            }
        break;        case 2:
            this.CTL += this.Step;
            if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
            {
                this.ID.scrollLeft += this.ScrollStep + this.Step - this.CTL;
                this.Pause();
                return;
            }
            else
            {
                if(this.ID.scrollLeft >= this.ClientScroll)
                {
                    this.ID.scrollLeft -= this.ClientScroll;
                }
                this.ID.scrollLeft += this.Step;
            }
        break;        case 3:
            this.CTL += this.Step;
            if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
            {
                this.ID.scrollLeft -= this.ScrollStep + this.Step - this.CTL;
                this.Pause();
                return;
            }
            else
            {
                if(this.ID.scrollLeft <= 0)
                {
                    this.ID.scrollLeft += this.ClientScroll;
                }
                this.ID.scrollLeft -= this.Step;
            }
        break;
    }
}

解决方案 »

  1.   

    代码比较长,只详细注释说明的上面的一部分,看懂了上面的下面的应该可以看懂吧,某些不理解的原型函数和属性可以自己百度一下
    this.ID = document.getElementById(arguments[0]);//获取页面一个元素对象,该元素的ID为函数的第一个参数(arguments[0]),arguments是函数的参数集合,索引从0开始
      if(!this.ID)//如果该元素不存在就直接返回
      {
      this.ID = -1;
      return;
      }
      //以下是变量的初始化
      this.Direction = this.Width = this.Height = this.DelayTime = this.WaitTime = this.Correct = this.CTL = this.StartID = this.Stop = this.MouseOver = 0;
      this.Step = 1;
      this.Timer = 30;
      this.DirectionArray = {"top":0 , "bottom":1 , "left":2 , "right":3};
      if(typeof arguments[1] == "number") this.Direction = arguments[1];//第二参数是方向
      if(typeof arguments[2] == "number") this.Step = arguments[2];//第三参数是步长
      if(typeof arguments[3] == "number") this.Width = arguments[3];//第四个参数是宽度
      if(typeof arguments[4] == "number") this.Height = arguments[4];//第五个参数是高度
      if(typeof arguments[5] == "number") this.Timer = arguments[5];//第六个参数是时间间隔
      if(typeof arguments[6] == "number") this.DelayTime = arguments[6];//第其个参数是延迟时间
      if(typeof arguments[7] == "number") this.WaitTime = arguments[7];//第八个参数是等待时间
      if(typeof arguments[8] == "number") this.ScrollStep = arguments[8];//第九个参数是滚动步长 
      this.ID.style.overflow = this.ID.style.overflowX = this.ID.style.overflowY = "hidden"; //设置元素的overflow样式为隐藏
      this.ID.noWrap = true;//禁止自动换行
      this.IsNotOpera = (navigator.userAgent.toLowerCase().indexOf("opera") == -1);//判断是不是opera浏览器
      if(arguments.length >= 7) this.Start();//如果参数个数大于等于7,那么运行start函数
    }//start开始函数定义
    Marquee.prototype.Start = function()
    {
      if(this.ID == -1)return;//元素不存在则直接返回
      if(this.WaitTime < 800)this.WaitTime = 800;//如果等待时间小于800则将其置为800
      if(this.Timer < 20)this.Timer = 20;//如果时间间隔小于20则将其置为20
      if(this.Width == 0)this.Width = parseInt(this.ID.style.width);//parseInt是转换为int型的方法
      if(this.Height == 0)this.Height = parseInt(this.ID.style.height);
      if(typeof this.Direction == "string")this.Direction = this.DirectionArray[this.Direction.toString().toLowerCase()];
      this.HalfWidth = Math.round(this.Width / 2);//round四舍五入
      this.BakStep = this.Step;
      this.ID.style.width = this.Width+'px';//必须添加'px',否则IE外的浏览器不支持//设置样式
      this.ID.style.height = this.Height+'px';//
      if(typeof this.ScrollStep != "number")this.ScrollStep = this.Direction > 1 ? this.Width : this.Height;//如果滚动步长不是一个数字,则方向参数是否大于1来决定滚动步长是等于宽度还是高度
      var msobj = this;
      var timer = this.Timer;
      var delaytime = this.DelayTime;
      var waittime = this.WaitTime;
      msobj.StartID = function(){msobj.Scroll()}//滚动调用事件
      msobj.Continue = function()
      {
      if(msobj.MouseOver == 1)
      {
      setTimeout(msobj.Continue,delaytime);//鼠标经过是做Continue事件  }
      else//鼠标不经过时做滚动事件(startID),即做scroll事件
      { clearInterval(msobj.TimerID);//clearInterval方法是清除setInterval的运行
      msobj.CTL = msobj.Stop = 0;
      msobj.TimerID = setInterval(msobj.StartID,timer);
      }
      }
      //暂停事件
      msobj.Pause = function()
      {
      msobj.Stop = 1;
      clearInterval(msobj.TimerID);
      setTimeout(msobj.Continue,delaytime);
      }
      //开始事件
      msobj.Begin = function()
      {
      msobj.ClientScroll = msobj.Direction > 1 ? msobj.ID.scrollWidth : msobj.ID.scrollHeight;
      if((msobj.Direction <= 1 && msobj.ClientScroll <msobj.Height) || (msobj.Direction > 1 && msobj.ClientScroll <msobj.Width))return;//不满足if中的条件则直接返回
      msobj.ID.innerHTML += msobj.ID.innerHTML;//copy 使ID元素中有两份同样的数据
      msobj.TimerID = setInterval(msobj.StartID,timer);//开始运行事件,隔timer常时间运行一次
      if(msobj.ScrollStep < 0)return;//步长小于0则直接返回
      msobj.ID.onmousemove = function(event)//绑定ID元素的鼠标移动事件
      {
      if(msobj.ScrollStep == 0 && msobj.Direction > 1)
      {
      var event = event || window.event;
      if(window.event)
      {
      if(msobj.IsNotOpera)//如果不是opera浏览器
      {   
      msobj.EventLeft = event.srcElement.id == msobj.ID.id ? event.offsetX - msobj.ID.scrollLeft : event.srcElement.offsetLeft - msobj.ID.scrollLeft + event.offsetX;//如果当前触发事件的对象元素是ID元素,EventLeft 等于鼠标相对于边框的横向坐标减去左滚动的长度,否则等于当前对象相对于父元素左边的距离减去左滚动长度再加上鼠标横坐标
      }
      else
      {
      msobj.ScrollStep = null;  return;
      }
      }
      else//下面是对其他浏览器的做法layerX鼠标的横坐标
      {
      msobj.EventLeft = event.layerX - msobj.ID.scrollLeft;
      }
      msobj.Direction = msobj.EventLeft > msobj.HalfWidth ? 3 : 2;
      msobj.AbsCenter = Math.abs(msobj.HalfWidth - msobj.EventLeft);
      msobj.Step = Math.round(msobj.AbsCenter * (msobj.BakStep*2) / msobj.HalfWidth);
      }
      }
      //绑定元素的鼠标经过事件
      msobj.ID.onmouseover = function()
      {
      if(msobj.ScrollStep == 0)return;
      msobj.MouseOver = 1;
      clearInterval(msobj.TimerID);
      }
      msobj.ID.onmouseout = function()
      {
      if(msobj.ScrollStep == 0)
      {
      if(msobj.Step == 0)msobj.Step = 1;
      return;
      }
      msobj.MouseOver = 0;
      if(msobj.Stop == 0)
      {
      clearInterval(msobj.TimerID);
      msobj.TimerID = setInterval(msobj.StartID,timer);
      }
      }
      }
      setTimeout(msobj.Begin,waittime);
    }
    //滚动事件定义
    Marquee.prototype.Scroll = function()
    {
      switch(this.Direction)
      {
      case 0:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
      {
      this.ID.scrollTop += this.ScrollStep + this.Step - this.CTL;
      this.Pause();
      return;
      }
      else
      {
      if(this.ID.scrollTop >= this.ClientScroll)
      {
      this.ID.scrollTop -= this.ClientScroll;
      }
      this.ID.scrollTop += this.Step;
      }
      break;  case 1:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
      {
      this.ID.scrollTop -= this.ScrollStep + this.Step - this.CTL;
      this.Pause();
      return;
      }
      else
      {
      if(this.ID.scrollTop <= 0)
      {
      this.ID.scrollTop += this.ClientScroll;
      }
      this.ID.scrollTop -= this.Step;
      }
      break;  case 2:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
      {
      this.ID.scrollLeft += this.ScrollStep + this.Step - this.CTL;
      this.Pause();
      return;
      }
      else
      {
      if(this.ID.scrollLeft >= this.ClientScroll)
      {
      this.ID.scrollLeft -= this.ClientScroll;
      }
      this.ID.scrollLeft += this.Step;
      }
      break;  case 3:
      this.CTL += this.Step;
      if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
      {
      this.ID.scrollLeft -= this.ScrollStep + this.Step - this.CTL;
      this.Pause();
      return;
      }
      else
      {
      if(this.ID.scrollLeft <= 0)
      {
      this.ID.scrollLeft += this.ClientScroll;
      }
      this.ID.scrollLeft -= this.Step;
      }
      break;
      }
    }