js实现单行文本向上滚动效果实例代码

 更新时间:2013年11月28日 10:07:29   作者:  
这篇文章主要介绍了js实现单行文本向上滚动效果,大家参考使用吧

复制代码 代码如下:

/***************滚动场次开始*****************/

function ScrollText(content, btnPrevious, btnNext, autoStart) {
    this.Delay = 10;
    this.LineHeight = 20;
    this.Amount = 1;
    this.Direction = "up";
    this.Timeout = 1500;
    this.ScrollContent = this.$(content);
    this.ScrollContent.innerHTML += this.ScrollContent.innerHTML;
    //this.ScrollContent.scrollTop = 0;
    if (btnNext) {
        this.NextButton = this.$(btnNext);
        this.NextButton.onclick = this.GetFunction(this, "Next");
        this.NextButton.onmouseover = this.GetFunction(this, "Stop");
        this.NextButton.onmouseout = this.GetFunction(this, "Start");
    }
    if (btnPrevious) {
        this.PreviousButton = this.$(btnPrevious);
        this.PreviousButton.onclick = this.GetFunction(this, "Previous");
        this.PreviousButton.onmouseover = this.GetFunction(this, "Stop");
        this.PreviousButton.onmouseout = this.GetFunction(this, "Start");
    }
    this.ScrollContent.onmouseover = this.GetFunction(this, "Stop");
    this.ScrollContent.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 () {
    clearTimeout(this.AutoScrollTimer);
    this.AutoScrollTimer = setTimeout(this.GetFunction(this, "AutoScroll"), this.Timeout);
}

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

ScrollText.prototype.AutoScroll = function () {
    if (this.Direction == "up") {
        if (parseInt(this.ScrollContent.scrollTop) >= parseInt(this.ScrollContent.scrollHeight) / 2) {
            this.ScrollContent.scrollTop = 0;
        }
        this.ScrollContent.scrollTop += this.Amount;
    } else {
        if (parseInt(this.ScrollContent.scrollTop) <= 0) {
            this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
        }
        this.ScrollContent.scrollTop -= this.Amount;
    }
    if (parseInt(this.ScrollContent.scrollTop) % this.LineHeight != 0) {
        this.ScrollTimer = setTimeout(this.GetFunction(this, "AutoScroll"), this.Delay);
    } else {
        this.AutoScrollTimer = setTimeout(this.GetFunction(this, "AutoScroll"), this.Timeout);
    }
}

ScrollText.prototype.Scroll = function (direction) {
    if (direction == "up") {
        if (this.ScrollContent.scrollTop == 0) {
            this.ScrollContent.scrollTop = parseInt(this.ScrollContent.scrollHeight) / 2;
        }
        this.ScrollContent.scrollTop -= this.Amount;
    } else {
        this.ScrollContent.scrollTop += this.Amount;
    }
    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.Delay);
    }
}

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

if (document.getElementById("ul_round")) {
    var scrollup = new ScrollText("ul_round");
    scrollup.LineHeight = 40;        //单排文字滚动的高度
    scrollup.Amount = 1;            //注意:子模块(LineHeight)一定要能整除Amount.
    scrollup.Delay = 30;           //延时
    scrollup.Start();             //文字自动滚动
    scrollup.Direction = "up";   //默认设置为文字向上滚动
}
/***************滚动场次结束*****************/

相关文章

  • JS给Textarea文本框添加行号的方法

    JS给Textarea文本框添加行号的方法

    这篇文章主要介绍了JS给Textarea文本框添加行号的方法,涉及javascript针对页面元素结点的读取与判定技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • js求数组最大值的八种具体实现方法

    js求数组最大值的八种具体实现方法

    数组如何求最大值,想必很多的朋友都不会吧,下面这篇文章主要给大家介绍了关于使用js求数组最大值的八种方法具体实现的相关资料,文中给出了详细的代码示例,需要的朋友可以参考下
    2023-09-09
  • JS猜数字游戏实例讲解

    JS猜数字游戏实例讲解

    这篇文章主要为大家详细介绍了JS猜数字游戏实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • js获取指定时间的前几秒

    js获取指定时间的前几秒

    本文主要介绍了根据一张图片的拍摄时间获取到这个时间前二后三的一个五秒钟的视频信息的实例方法。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04
  • JS库之Three.js 简易入门教程(详解之一)

    JS库之Three.js 简易入门教程(详解之一)

    three.js是一款webGL框架,由于其易用性被广泛应用。下面脚本之家小编通过案例给大家阐述three.js的基本配置方法,具体内容详情大家参考下本文吧
    2017-09-09
  • javascript移动设备Web开发中对touch事件的封装实例

    javascript移动设备Web开发中对touch事件的封装实例

    这篇文章主要介绍了javascript移动设备Web开发中对touch事件的封装实例,分别对tap事件、doubleTap事件、longTap事件、swipe事件做了封装,需要的朋友可以参考下
    2014-06-06
  • Echart结合圆形实现仪表盘的绘制详解

    Echart结合圆形实现仪表盘的绘制详解

    EChart开源来自百度商业前端数据可视化团队,基于html5 Canvas,是一个纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。本文将利用EChart实现仪表盘的绘制,感兴趣的可以学习一下
    2022-03-03
  • canvas绘制环形进度条

    canvas绘制环形进度条

    本文主要介绍了canvas绘制环形进度条的示例代码,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • 使用JavaScript监视有没有被刷新后跳转其他页面

    使用JavaScript监视有没有被刷新后跳转其他页面

    这篇文章主要为大家详细介绍了如何使用JavaScript监视有没有被刷新后跳转其他页面,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下
    2025-01-01
  • JavaScript可折叠区域实现代码

    JavaScript可折叠区域实现代码

    可折叠区域的基本思想:通过点击某个地方来显示或隐藏屏幕中的某个区域。
    2010-10-10

最新评论