用javascrpt实现淘宝首页的间隔性滚动效果

文/robot  出处/博客园

用法很简单:
var scroll = new ScrollText("content","pre","next",true);
第一个参数是滚动区的id,第二个是显示上一条的按钮的id,第三个是显示下一条的按钮的id,第四个是是否直接开始滚动,为false的话后面再scroll.Start()就OK了。
废话不说了,贴代码吧。
我第一次发贴,不懂排版,直接贴了。
ScrollText.js



引用:
function ScrollText(content,btnPrevious,btnNext,autoStart)
{
    this.Speed = 10;
    this.Timeout = 1500;
    this.LineHeight = 20;
    this.NextButton = this.$(btnNext);
    this.PreviousButton = this.$(btnPrevious);
    this.ScrollContent = this.$(content);
    this.ScrollContent.innerHTML += this.ScrollContent.innerHTML;
    this.NextButton.onclick = this.GetFunction(this,"Next");
    this.PreviousButton.onclick = this.GetFunction(this,"Previous");
   
    this.NextButton.onmouseover = this.GetFunction(this,"Stop");
    this.NextButton.onmouseout = this.GetFunction(this,"Start");
   
    this.ScrollContent.onmouseover = this.GetFunction(this,"Stop");
    this.ScrollContent.onmouseout = this.GetFunction(this,"Start");
   
    this.PreviousButton.onmouseover = this.GetFunction(this,"Stop");
    this.PreviousButton.onmouseout = this.GetFunction(this,"Start");
    if(autoStart)
    {
        this.Start();
    }
}

ScrollText.prototype.$ = function(element)
{
    return document.getElementById(element);
}

ScrollText.prototype.Previous = function()
{
    clearTimeout(this.AutoScrollTimer);
    clearTimeout(this.ScrollTimer);
    this.Scroll("up");
}

ScrollText.prototype.Next = function()
{
    clearTimeout(this.AutoScrollTimer);
    clearTimeout(this.ScrollTimer);
    this.Scroll("down");
}

ScrollText.prototype.Start = function()
{
    this.AutoScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Timeout);
}

ScrollText.prototype.Stop = function()
{
    clearTimeout(this.AutoScrollTimer);
}

ScrollText.prototype.AutoScroll = function()
{
    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;
        }
        this.AutoScrollTimer = setTimeout(this.GetFunction(this,"AutoScroll"), this.Timeout);
    }
}

ScrollText.prototype.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;
        }
    if(parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0)
    {
        this.ScrollTimer = setTimeout(this.GetFunction(this,"Scroll",direction), this.Speed);
    }
}

ScrollText.prototype.GetFunction = function(variable,method,param)
{
    return function()
    {
        variable[method](param);
    }
}


Demo.Htm




引用:
<html xmlns="http://www.w3.org/1999/xhtml"; >
<head>
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript" src="ScrollText.js"></script>
</head>
<body>
<div id="pre" style="width:20px;height:20px;float:left;text-align:center;background-color:#EEEEEE;border:solid 1px #888888;">↑</div>
<div id="content" style="height:20px;line-height:20px;overflow:hidden;float:left;width:120px;border:solid 1px #0000FF;">
hello1........<br>
hello2........<br>
hello3........<br>
hello4........<br>
hello5........<br>
hello6........<br>
hello7........<br>
hello8........<br>
</div>
<div id="next" style="width:20px;height:20px;float:left;text-align:center;background-color:#EEEEEE;border:solid 1px #888888;">↓</div>
<script language="javascript" type="text/javascript">
var scroll = new ScrollText("content","pre","next",true);
</script>
</body>
</html>


功能比较简单,用得着的就拿去吧。源码下载:
 附件: 您所在的用户组无法下载或查看附件

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [javascript] 的这些文章也感兴趣:

JavaScript判断浏览器类型及版本
JavaScript极速狂飙:组合拼接字符串的效率
基于对象的JavaScript语言
将js文件编译成动态链接库(dll)文件
用javascript实现(页面正在加载的效果)
Gb2312转utf-8(vbs js)
利用Yahoo! Search API开发自已的搜索引擎-javascript版
如何利用IE进行JavaScript脚本调试
用javascript来实现一个图片浏览器
中文的版用JavaScript实现超酷的“网页时钟”