简体   繁体   English

Javascript新闻滚动条

[英]Javascript News Scroller

see the news scroller on the top of this site 请参阅本网站顶部的新闻滚动条

http://track.dc.gov/Agency/DH0 http://track.dc.gov/Agency/DH0

Any idea what library/functions this site uses to implment such a smooth scroller? 知道这个站点使用什么库/函数来实现这种平滑的滚动条吗?

They have a very nicely formatted block of code you can study. 他们有一个很好地格式化的代码块,您可以研究。 Open your favorite JS debugger when you visit the site, wait for everything to get moving, and then press "Break All" or the equivalent in your debugger. 当您访问站点时,打开您喜欢的JS调试器,等待所有内容移动,然后在调试器中按“全部破坏”或等效按钮。 You'll see something like the following: 您会看到类似以下内容:

Dashboard.UI.EndlessLine = function() {
    var me = this;
    me.jq = $(me);
    me.classNames = { CONTAINER: "uiEndless", VIEW: "uiEndlessView", CANVAS: "uiEndlessCanvas", TILE: "uiEndlessTile" };
    var canvas = null;
    var view = null;
    var tiles = null;
    var x = 0;
    var xx = 0;
    var canvasWidth = 0;
    var step = 1;
    var delay = 40;
    me.initialize = function(container, data, handler) {
        required(container, "container");
        required(data, "data");
        required(handler, "handler");
        container.addClass(me.classNames.CONTAINER);
        view = newDiv(me.classNames.VIEW);
        canvas = newDiv(me.classNames.CANVAS);
        view.append(canvas);
        container.append(view);
        x = 0;
        xx = 0;
        canvasWidth = 0;
        tiles = me.populateTiles(data, handler);
        container.click(function() {
            if (me.started()) me.stop(); else me.start();
        });
    };
    me._resize = function(size) {
    };
    var moveId = 0;
    me.start = function() {
        me.stop();
        me.tick();
    }
    me.stop = function() {
        if (moveId > 0) clearTimeout(moveId);
        moveId = 0;
    }
    me.started = function() {
        return moveId > 0;
    };
    me.tick = function() {
        var tile = tiles.current();
        var width = tile.calculatedWidth;
        if (x < width - step) {
            x += step;
        } else {
            x = 0;
            tile.css("left", canvasWidth + "px");
            if (tiles.advance()) {
                xx = 0;
                canvasWidth = 0;
                do {
                    current = tiles.current();
                    width = current.calculatedWidth;
                    current[0].style.left = canvasWidth + "px";
                    canvasWidth += width;
                } while (!tiles.advance());
            } else {
                canvasWidth += width;
            }
        }
        canvas[0].style.left = -(xx) + "px";
        xx += step;
        moveId = setTimeout(me.tick, delay);
    }
    me.populateTiles = function(data, handler) {
        var tiles = new Dashboard.Core.List();
        var viewWidth = view.contentWidth();
        var maxHeight = 0;
        each(data, function() {
            var tile = newDiv(me.classNames.TILE);
            handler.call(this, tile);
            tile.css({ left: canvasWidth + "px", top: 0 });
            canvas.append(tile);
            var width = tile.outerWidth();
            var height = tile.outerHeight();
            if (maxHeight < height) maxHeight = height;
            tile.calculatedWidth = width;
            canvasWidth += width; // getting width may only be done after the element is attached to DOM
            tiles.append(tile);
            view.height(height);
        });
        return tiles.createCycle();
    }
}

I'm impressed -- everything looks professional and nicely namespaced. 我印象深刻-一切看起来都很专业,并且命名空间很好。

Update : If you want an explanation of how it works, focus on the tick method defined above. 更新 :如果要解释其工作原理,请着重于上面定义的tick方法。 Glossing over all the details (cause I haven't really studied it myself), it calculates a step size, moves the message element to the left by the some amount, and schedules the next tick call for 40 milliseconds in the future. 对所有细节进行光泽处理(因为我自己还没有真正研究过它),它计算步长,将message元素向左移动一定量,并将下一个刻度调用安排在将来的40毫秒内。

jQuery enthusiast, Remy Sharp, has his own Marquee Plugin that you can implement pretty easily. jQuery爱好者Remy Sharp拥有自己的Marquee插件,您可以轻松实现。 You can gather deeper details of it on his blog or by visiting the demo page . 您可以在他的博客上或访问演示页面来收集更详细的信息。

For Mootools users, there's Mooquee . 对于Mootools用户,有Mooquee

You can also view the actual code for this example online at http://track.dc.gov/Resource/Script/ - do a search for "uiEndless" to find the target-scripting. 您也可以在http://track.dc.gov/Resource/Script/上在线查看此示例的实际代码-搜索“ uiEndless”以找到目标脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM