﻿//参数说明:
//demo  字幕区域标签(div)的ID;
//demo1/demo2  显示内容标签(div或td)的ID   demo1为原始内容,demo2是它的拷贝;
//direction 字幕方向(up,down,left,right) ;
//delay 字幕播放的延迟时间(毫秒);
//step 字幕播放的步长(即pix,步长越小,如step=1,滚动越平滑)

function Marquee (demo, demo1, demo2, direction, delay, step)

{

direction = direction.toLowerCase();

if(((direction == "up" || direction == "down") &&
($(demo1).offsetHeight > $(demo).offsetHeight)) || ((direction ==
"left" || direction == "right") && ($(demo1).offsetWidth >
$(demo).offsetWidth)))

{

  $(demo2).innerHTML = $(demo1).innerHTML;

  if(direction == "down")

   $(demo).scrollTop = 2 * $(demo1).offsetHeight - $(demo).offsetHeight;

  if(direction == "right")

   $(demo).scrollLeft = 2 * $(demo1).offsetWidth - $(demo).offsetWidth;

}

else

  return;

 

var flag = true;

var speed = delay == null? 1 : parseInt(delay);

var amount = step == null? 1 : parseInt(step);

var Marquee = function ()

{

  switch(direction)

  {

   case "up":

    if($(demo2).offsetTop - $(demo).scrollTop <= 0)

     $(demo).scrollTop -= $(demo1).offsetHeight;

    else

     $(demo).scrollTop += amount;

    break;

   case "down":

    if($(demo1).offsetTop - $(demo).scrollTop >= 0)

     $(demo).scrollTop += $(demo2).offsetHeight;

    else

     $(demo).scrollTop -= amount;

    break;

   case "left":

    if($(demo2).offsetWidth - $(demo).scrollLeft <= 0)

     $(demo).scrollLeft -= $(demo1).offsetWidth;

    else

     $(demo).scrollLeft += amount;

    break;

   case "right":

    if($(demo).scrollLeft <= 0)

     $(demo).scrollLeft += $(demo2).offsetWidth;

    else

     $(demo).scrollLeft -= amount;

    break;

   default:break;

  }

}

var timer = setInterval(Marquee,speed);

var play = function ()

{

  if(flag)

  {

   clearInterval(timer);

   timer = setInterval(Marquee, speed);

  }

}

$(demo).onmouseover = function ()

{

  if(flag)

   clearInterval(timer);

}

$(demo).onmouseout = function ()

{

  if(flag)

   timer = setInterval(Marquee, speed);

}

this.delay = function (s)

{

  speed = s == null? 50 : parseInt(s);

  play();

}

this.step = function (s)

{

  amount = s == null? 1 : parseInt(s);

  play();

}

this.start = function ()

{

  if(!flag)

  {

   flag = true;

   play();

  }

}

this.stop = function ()

{

  if(flag)

  {

   flag = false;

   clearInterval(timer);

  }

}

this.direction = function (s)

{

  s = s.toLowerCase();

  if( s == direction )

   return;

  if(s == "down" && direction == "up" )

   direction = s;

  if(s == "up" && direction == "down")

   direction = s;

  if(s == "right" && direction == "left")

   direction = s;

  if(s == "left" && direction == "right")

   direction = s;

  if (s == direction)

   play();

}

}


