需要帮忙。作者已经放弃修复这个jQuery插件!

时间:2010-07-15 01:43:09

标签: javascript jquery jquery-plugins

我正试图从http://www.makemineatriple.com/2007/10/bbcnewsticker

实现这个jQuery新闻自动收报机样式插件

就像评论中提到的那样(5月左右)有一个错误,作者失去了修复错误的意愿。

错误是: 在Mac浏览器(Firefox,Opera和Safari,所有OSX)中 - 链接(一个href)在每个列表项完成滚动/显示之前不“工作”。基本上,在加载此插件后,所有a href都停止工作。

以下是插件的代码(http://plugins.jquery.com/project/BBCnewsTicker):

/*
News ticker plugin (BBC news style)
Bryan Gullan,2007-2010
version 2.2
updated 2010-04-04
Documentation at http://www.makemineatriple.com/news-ticker-documentation/
Demo at http://www.makemineatriple.com/jquery/?newsTicker
Use and distrubute freely with this header intact.
*/

(function($) {

    var name='newsTicker';

    function runTicker(settings) {

        tickerData = $(settings.newsList).data('newsTicker');

        if(tickerData.currentItem > tickerData.newsItemCounter){
            // if we've looped to beyond the last item in the list, start over
            tickerData.currentItem = 0;
        }
        else if (tickerData.currentItem < 0) {
            // if we've looped back before the first item, move to the last one
            tickerData.currentItem = tickerData.newsItemCounter;
        }

        if(tickerData.currentPosition == 0) {
            if(tickerData.newsLinks[tickerData.currentItem].length > 0) {
                $(tickerData.newsList).empty().append('<li><a href="'+ tickerData.newsLinks[tickerData.currentItem] +'"></a></li>');
            }
            else {
                $(tickerData.newsList).empty().append('<li></li>');
            }
        }

        //only start the ticker itself if it's defined as animating: otherwise it's paused or under manual advance
        if (tickerData.animating) {

            if( tickerData.currentPosition % 2 == 0) {
                    var placeHolder = tickerData.placeHolder1;
            }
            else {
                var placeHolder = tickerData.placeHolder2;
            }

            if( tickerData.currentPosition < tickerData.newsItems[tickerData.currentItem].length) {
                // we haven't completed ticking out the current item

                var tickerText = tickerData.newsItems[tickerData.currentItem].substring(0,tickerData.currentPosition);
                if(tickerData.newsLinks[tickerData.currentItem].length > 0) {
                    $(tickerData.newsList + ' li a').text(tickerText + placeHolder);
                }
                else {
                    $(tickerData.newsList + ' li').text(tickerText + placeHolder);
                }
                tickerData.currentPosition ++;
                setTimeout(function(){runTicker(settings); settings = null;},tickerData.tickerRate);
            }

            else {
                // we're on the last letter of the current item

                if(tickerData.newsLinks[tickerData.currentItem].length > 0) {
                    $(tickerData.newsList + ' li a').text(tickerData.newsItems[tickerData.currentItem]);
                }
                else {
                    $(tickerData.newsList + ' li').text(tickerData.newsItems[tickerData.currentItem]);
                }

                setTimeout(function(){
                    if (tickerData.animating) {
                        tickerData.currentPosition = 0;
                        tickerData.currentItem ++;
                        runTicker(settings); settings = null;
                    }
                },tickerData.loopDelay);

            }
        }

        else {// settings.animating == false 

            // display the full text of the current item
            var tickerText = tickerData.newsItems[tickerData.currentItem];

            if(tickerData.newsLinks[tickerData.currentItem].length > 0) {
                $(tickerData.newsList + ' li a').text(tickerText);
            }
            else {
                $(tickerData.newsList + ' li').text(tickerText);
            }

        }

    }


    // Core plugin setup and config
    jQuery.fn[name] = function(options) {

        // Add or overwrite options onto defaults
        var settings = jQuery.extend({}, jQuery.fn.newsTicker.defaults, options);

        var newsItems = new Array();
        var newsLinks = new Array();
        var newsItemCounter = 0;

        // Hide the static list items
        $(settings.newsList + ' li').hide();

        // Store the items and links in arrays for output
        $(settings.newsList + ' li').each(function(){
            if($(this).children('a').length) {
                newsItems[newsItemCounter] = $(this).children('a').text();
                newsLinks[newsItemCounter] = $(this).children('a').attr('href');
            }
            else {
                newsItems[newsItemCounter] = $(this).text();
                newsLinks[newsItemCounter] = '';
            }
            newsItemCounter ++;
        });

        var tickerElement = $(settings.newsList); // for quick reference below

        tickerElement.data(name, {
            newsList: settings.newsList,
            tickerRate: settings.tickerRate,
            startDelay: settings.startDelay,
            loopDelay: settings.loopDelay,
            placeHolder1: settings.placeHolder1,
            placeHolder2: settings.placeHolder2,
            controls: settings.controls,
            ownControls: settings.ownControls,
            stopOnHover: settings.stopOnHover,
            newsItems: newsItems,
            newsLinks: newsLinks,
            newsItemCounter: newsItemCounter - 1, // -1 because we've incremented even after the last item (above)
            currentItem: 0,
            currentPosition: 0,
            firstRun:1
        })
        .bind({
            stop: function(event) {
                // show remainder of the current item immediately
                tickerData = tickerElement.data(name);
                if (tickerData.animating) { // only stop if not already stopped
                    tickerData.animating = false;
                }
            },
            play: function(event) {
                // show 1st item with startdelay
                tickerData = tickerElement.data(name);
                if (!tickerData.animating) { // if already animating, don't start animating again
                    tickerData.animating = true;
                    setTimeout(function(){runTicker(tickerData); tickerData = null;},tickerData.startDelay);
                }
            },
            resume: function(event) {
                // start from next item, with no delay
                tickerData = tickerElement.data(name);
                if (!tickerData.animating) { // if already animating, don't start animating again
                    tickerData.animating = true;
                    // set the character position as 0 to ensure on resume we start at the right point
                    tickerData.currentPosition = 0;
                    tickerData.currentItem ++;
                    runTicker(tickerData); // no delay when resuming.
                }
            },
            next: function(event) {
                // show whole of next item
                tickerData = tickerElement.data(name);
                // stop (which sets as non-animating), and call runticker
                $(tickerData.newsList).trigger("stop");
                // set the character position as 0 to ensure on resume we start at the right point
                tickerData.currentPosition = 0;
                tickerData.currentItem ++;
                runTicker(tickerData);
            },
            previous: function(event) {
                // show whole of previous item
                tickerData = tickerElement.data(name);
                // stop (which sets as non-animating), and call runticker
                $(tickerData.newsList).trigger("stop");
                // set the character position as 0 to ensure on resume we start at the right point
                tickerData.currentPosition = 0;
                tickerData.currentItem --;
                runTicker(tickerData);
            }
        });     
        if (settings.stopOnHover) {
            tickerElement.bind({                    
                mouseover: function(event) {
                    tickerData = tickerElement.data(name);
                    if (tickerData.animating) { // stop if not already stopped
                        $(tickerData.newsList).trigger("stop");
                        if (tickerData.controls) { // ensure that the ticker can be resumed if controls are enabled
                            $('.stop').hide();
                            $('.resume').show();
                        }
                    }
                }
            });
        }

        tickerData = tickerElement.data(name);

        // set up control buttons if the option is on
        if (tickerData.controls || tickerData.ownControls) {
            if (!tickerData.ownControls) {
                $('<ul class="ticker-controls"><li class="play"><a href="#play">Play</a></li><li class="resume"><a href="#resume">Resume</a></li><li class="stop"><a href="#stop">Stop</a></li><li class="previous"><a href="#previous">Previous</a></li><li class="next"><a href="#next">Next</a></li></ul>').insertAfter($(tickerData.newsList));
            }
            $('.play').hide();
            $('.resume').hide();

            $('.play').click(function(event){
                $(tickerData.newsList).trigger("play");
                $('.play').hide();
                $('.resume').hide();
                $('.stop').show();
                event.preventDefault();
            });
            $('.resume').click(function(event){
                $(tickerData.newsList).trigger("resume");
                $('.play').hide();
                $('.resume').hide();
                $('.stop').show();
                event.preventDefault();
            });
            $('.stop').click(function(event){
                $(tickerData.newsList).trigger("stop");
                $('.stop').hide();
                $('.resume').show();
                event.preventDefault();
            });
            $('.previous').click(function(event){
                $(tickerData.newsList).trigger("previous");
                $('.stop').hide();
                $('.resume').show();
                event.preventDefault();
            });
            $('.next').click(function(event){
                $(tickerData.newsList).trigger("next");
                $('.stop').hide();
                $('.resume').show();
                event.preventDefault();
            });

        };

        // tell it to play
        $(tickerData.newsList).trigger("play");
    };

    // News ticker defaults 
    jQuery.fn[name].defaults = {
        newsList: "#news",
        tickerRate: 80,
        startDelay: 100,
        loopDelay: 3000,
        placeHolder1: " |",
        placeHolder2: "_",
        controls: true,
        ownControls: false,
        stopOnHover: true
    }

})(jQuery);

任何解决方案?我不是程序员,所以如果有人能指出在哪里修补它非常感谢!

更新: 似乎只有链接?标记变为禁用。 示例:http://url.com/blog/index.html?page=2

2 个答案:

答案 0 :(得分:1)

我碰巧碰到过这篇文章。我仍然支持自动收报机,自去年7月以来已有一些版本。

缓解此问题的方法是,现在有一个“停止悬停”选项,当用户将鼠标悬停在其上时(包括当然即将点击它),该选项会暂停自动收报机并完成(立即)项目的显示)。

如果这仍然与您有关,如果您仍然遇到最新版本的问题,则值得通过评论主题阅读;如果你还有问题,请联系我们(如果其中一条评论是你的,我错过了,那么对不起!)。 “官方”方式是在jQuery插件网站上发布错误报告,该报告完全跟踪任何报告的问题,但我确实尝试回复通过博客请求支持的任何人。

答案 1 :(得分:0)

如果您的文档中有任何ID为news的元素,可能会发生冲突......这可能是这种情况吗?我会在你的html文档中搜索id="news"的任何出现并更正它们,看到好像将适当的参数传递给插件可能需要一些额外的研究。