ifeng.com 的文字滚动类

做网站时经常要用到一些滚动效果,看到凤凰网的不错就收藏一下,备用

 

方法:

function ScrollText(content,btnPrevious,btnNext,autoStart,timeout,isSmoothScroll)
{
this.Speed = 10;
this.Timeout = timeout;
this.stopscroll =false;//是否停止滚动的标志位
this.isSmoothScroll= isSmoothScroll;//是否平滑连续滚动
this.LineHeight = 20;//默认高度。可以在外部根据需要设置
this.NextButton = this.$(btnNext);
this.PreviousButton = this.$(btnPrevious);
this.ScrollContent = this.$(content);
this.ScrollContent.innerHTML += this.ScrollContent.innerHTML;//为了平滑滚动再加一遍

if(this.PreviousButton)

{
this.PreviousButton.onclick = this.GetFunction(this,"Previous");
this.PreviousButton.onmouseover = this.GetFunction(this,"MouseOver");
this.PreviousButton.onmouseout = this.GetFunction(this,"MouseOut");
}
if(this.NextButton){
this.NextButton.onclick = this.GetFunction(this,"Next");
this.NextButton.onmouseover = this.GetFunction(this,"MouseOver");
this.NextButton.onmouseout = this.GetFunction(this,"MouseOut");
}
this.ScrollContent.onmouseover = this.GetFunction(this,"MouseOver");
this.ScrollContent.onmouseout = this.GetFunction(this,"MouseOut");

if(autoStart)
{
this.Start();
}
}

ScrollText.prototype = {

$:function(element)
{
return document.getElementById(element);
},
Previous:function()
{
this.stopscroll = true;
this.Scroll("up");
},
Next:function()
{
this.stopscroll = true;
this.Scroll("down");
},
Start:function()
{
if(this.isSmoothScroll)
{
this.AutoScrollTimer = setInterval(this.GetFunction(this,"SmoothScroll"), this.Timeout);
}
else
{
this.AutoScrollTimer = setInterval(this.GetFunction(this,"AutoScroll"), this.Timeout);
}
},
Stop:function()
{
clearTimeout(this.AutoScrollTimer);
this.DelayTimerStop = 0;
},
MouseOver:function()
{
this.stopscroll = true;
},
MouseOut:function()
{
this.stopscroll = false;
},
AutoScroll:function()
{
if(this.stopscroll)
{
return;
}
this.ScrollContent.scrollTop++;
if(parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0)
{
this.ScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Speed);
}
else
{
if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2)
{
this.ScrollContent.scrollTop = 0;
}
clearTimeout(this.ScrollTimer);
//this.AutoScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Timeout);
}
},
SmoothScroll:function()
{
if(this.stopscroll)
{
return;
}
this.ScrollContent.scrollTop++;
if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2)
{
this.ScrollContent.scrollTop = 0;
}
},
Scroll:function(direction)
{

if(direction=="up")
{
this.ScrollContent.scrollTop--;
}
else
{
this.ScrollContent.scrollTop++;
}

if(parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2)
{
this.ScrollContent.scrollTop = 0;
}
else if(parseInt(this.ScrollContent.scrollTop)<=0)
{
this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
}

if(parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0)
{
this.ScrollTimer = setTimeout(this.GetFunction(this,"Scroll",direction), this.Speed);
}
},
GetFunction:function(variable,method,param)
{
return function()
{
variable[method](param);
}
}
}
===========================================================
调用:

var scroll = new ScrollText(content,btnPrevious,btnNext,autoStart,timeout,isSmoothScroll)
注:
content:滚动对象ID
btnPrevious:向上滚动按钮ID
btnNext:向后滚动按钮ID
autoStart:是否自动开始 值 true false
timeout:滚动间隔时间
isSmoothScroll:是否平滑连续滚动


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值