无缝滚动文本

时间:2015-10-19 05:29:04

标签: jquery html css

我正在尝试实现类似于新闻自动收录器的功能,除了我不知道正在滚动的文本的大小。文本的结尾必须紧跟文本的开头(包装)。我当前的解决方案涉及到我复制文本以使其看起来无限,但我遇到问题让它看起来无缝。

目前我有这个: http://jsfiddle.net/theintellects/e7td1g0w/1/

有问题的代码:

    var containerwidth = $ticker.width();
    var left = containerwidth;
    var width = $tickerText.width();    
    function tick() {
        if (--left < -width) {
            left = 0;
        }
        $tickerText.css("margin-left", left + "px");
        setTimeout(tick, settings.speed);
    }
    tick();

你会注意到文本环绕但有一个断点,我重置了左边距,并导致“跳转”。有没有办法使这个无缝?我宁愿不要继续将字符串附加到自身并允许它永远向左滚动。

2 个答案:

答案 0 :(得分:2)

你的tick函数中的

不要设置为0,只是注释该行。

&#13;
&#13;
    $.fn.ticker = function (options) {
        'use strict';

        var settings = $.extend({}, $.fn.ticker.defaults, options);
        var $ticker = $(this);
        var tickerdata = {
            'content': 'I have some news for you, here is some breaking news that is looping around and around on and on and never seems to end. But what if it ends?'
        };

        var docfragment = document.createDocumentFragment();
        var $tickerText = $('<span/>', {
            class: 'ticker-text',
            html: tickerdata.content
        }).appendTo(docfragment);
        $ticker.append(docfragment);

        var containerwidth = $ticker.width();
        var left = containerwidth;
        var width = $tickerText.width();

        function tick() {
               if (--left< -width) {
                    left = containerwidth;//try setting it with container width
               }
            $tickerText.css("margin-left", left + "px");
            setTimeout(tick, settings.speed);
        }
        tick();
        return this;
    }

    $.fn.ticker.defaults = {
        speed: 11//change this for good visibility
    };
	$('.ticker').ticker();
&#13;
.ticker {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 20px;
    width: 100%;
    background-color: blue;
    color: yellow;
    display: inline-block;
}
.ticker .ticker-text {
    top: 0;
    height: 100%;
    padding: 2px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ticker"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试在.animate()功能

中使用tick
function tick() {
    return $tickerText.css("marginLeft", left * 2)
        .animate({
        marginLeft: "-" + (left * 2) + "px"
    }, 3000, "linear", tick)
}

&#13;
&#13;
$.fn.ticker = function(options) {
      'use strict';

      var settings = $.extend({}, $.fn.ticker.defaults, options);
      var $ticker = $(this);
      var tickerdata = {
        'content': 'I have some news for you, here is some breaking news that is looping around and around on and on and never seems to end. But what if it ends?'
      };

      var docfragment = document.createDocumentFragment();
      var $tickerText = $('<span/>', {
        class: 'ticker-text',
        html: tickerdata.content
      }).appendTo(docfragment);
      $ticker.append(docfragment);

      var containerwidth = $ticker.width();
      var left = containerwidth;
      var width = $tickerText.width();

      function tick() {
        return $tickerText.css("marginLeft", left * 2)
          .animate({
            marginLeft: "-" + (left * 2) + "px"
          }, settings.duration, "linear", tick)
      }
      tick();
      return this;
    }

    $.fn.ticker.defaults = {
      speed: 1,
      duration: 5000
    };
    $('.ticker').ticker({duration:10000});
&#13;
.ticker {
  position: fixed;
  bottom: 0;
  left: 0;
  height: 20px;
  width: 100%;
  background-color: blue;
  color: yellow;
  display: inline-block;
}
.ticker .ticker-text {
  top: 0;
  height: 100%;
  padding: 2px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="ticker"></div>
&#13;
&#13;
&#13;

jsfiddle http://jsfiddle.net/3ez9e81o/2/