JS类

本文介绍了一个使用JavaScript实现的滚动效果,通过创建并克隆DOM元素达到无缝滚动的目的。该实现支持自定义滚动方向和速度。
/**
* @author 靖鸣君
* @email jingmingjun92@163.com
* @description 滚动
* @class Marquee
* @param {Object}
*/
var Marquee = function(){
    this.direction = "top";
    this.speed = 30;
};

Marquee.prototype = {
    //initial
    init: function(obj, setttings){
        this.obj = obj;
        this._createBox();
        this.scroll();
        if(settings){
            settings.direction && (this.direction = settings.direction);
            settings.speed && (this.speed = settings.speed);
        }
    },
    _createBox: function(){
        //create inner box A
        this.iBox = document.createElement("div");
        var iBox = this.iBox;
        with(iBox.style){
            width = "100%";
            height = "100%";
            overflow = "hidden";
        }
        iBox.setAttribute("id", "marqueeBoxA");
        iBox.innerHTML = obj.innerHTML;

        //clone inner box B
        var iBox2 = iBox.cloneNode(true);
        iBox2.setAttribute("id", "marqueeBoxB");

        //append to obj Box
        this.obj.innerHTML = "";
        this.obj.appendChild(iBox);
        this.obj.appendChild(iBox2);
    },
    //animation
    scroll: function() {
        var _self = this;
        this.timer = setInterval(function(){
            _self._ani();
        }, this.speed);
    },
    //setInterval function
    _ani: function() {
        if(obj.clientHeight - obj.scrollTop <= 0){
            obj.scrollTop = obj.offsetHeight - obj.scrollTop + 1;
        } else {
            obj.scrollTop++;
            console.log(obj.offsetHeight, obj.scrollTop);
        }
    },
    stop: function(){
        clearInterval(this.timer);
    },
    start: function(){
        this.scroll();
    }
};

转载于:https://my.oschina.net/u/180293/blog/988683

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值