javascript - News Ticker - 提前重启

标签 javascript jquery html marquee ticker

我正在尝试让这个新闻自动收报机工作。出于某种原因,它将在第二个列表项之后重新启动,这不是我想要的 - 我希望它在列表项中循环直到最后。这个脚本有什么问题?

这是一个 JSFiddle

HTML

<h1>This runs differently than the ticker for some reason. It's highly annoying in my personal opinion.</h1>
<div id="ticker">    
    <div class="event">test1</div>
    <div class="event">test2</div>
    <div class="event">test3</div>
    <div class="event">test4</div>
</div>​

CSS

.event{float: left; width: 100px;}​

Javascript/Jquery

(function($) {
        $.fn.textWidth = function(){
             var calc = '<span style="display:none">' + $(this).text() + '</span>';
             $('body').append(calc);
             var width = $('body').find('span:last').width();
             $('body').find('span:last').remove();
            return width;
        };

        $.fn.marquee = function(args) {
            var that = $(this);
            var textWidth = that.textWidth(),
                offset = that.width(),
                width = offset,
                css = {
                    'text-indent' : that.css('text-indent'),
                    'overflow' : that.css('overflow'), 
                    'white-space' : that.css('white-space')
                },
                marqueeCss = {
                    'text-indent' : width,
                    'overflow' : 'hidden',
                    'white-space' : 'nowrap'
                },
                args = $.extend(true, { count: -1, speed: 1e1, leftToRight: false }, args),
                i = 0,
                stop = textWidth*-1,
                dfd = $.Deferred();

            function go() {
                if (that.data('isStopped') != 1)
                {
                if(!that.length) return dfd.reject();
                if(width == stop) {
                    i++;
                    if(i == args.count) {
                        that.css(css);
                        return dfd.resolve();
                    }
                    if(args.leftToRight) {
                        width = textWidth*-1;
                    } else {
                        width = offset;
                    }
                }
                that.css('text-indent', width + 'px');
                if(args.leftToRight) {
                    width++;
                } else {
                    width--;
                }
            }

                setTimeout(go, 10);
            };
            if(args.leftToRight) {
                width = textWidth*-1;
                width++;
                stop = offset;
            } else {
                width--;            
            }
            that.css(marqueeCss);
            that.data('isStopped', 0);
            that.bind('mouseover', function() { $(this).data('isStopped', 1); }).bind('mouseout', function() { $(this).data('isStopped', 0); });
            go();
            return dfd.promise();
        };        
    })(jQuery);
        $('h1').marquee();
        $('#ticker').marquee();​

最佳答案

放在容器div里就可以了

<div id="ticker-container">
<h1>This runs differently than the ticker for some reason. It's highly annoying in my personal opinion.</h1> <div id="ticker">    
    <div class="event">test1</div>
    <div class="event">test2</div>
    <div class="event">test3</div>
    <div class="event">test4</div>
</div>
</div>

$('#ticker-container').marquee();

关于javascript - News Ticker - 提前重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10576995/

相关文章:

javascript - 如何在 Chrome 扩展中使用本地存储

JavaScript 日期 : a nightmare

javascript - 取消选择内容可编辑元素

javascript - 在饼图中显示值

javascript - 如何抓取存储在html页面中的图像

javascript - window.location.href 到子文件夹

javascript - 每 x 分钟/小时重置一次计时器

php - 更改选择选项值时网页上偶尔显示白色空白

jquery - 不支持的浏览器上的 html5 必需属性

javascript - 滑动侧边栏菜单在 chrome 中不起作用