简体   繁体   English

如何解决此JavaScript新闻水平行情记录?

[英]How can i fix this javascript news horizontal ticker?

There is a problem in my code and can't seem to find it. 我的代码中有问题,似乎找不到。 My news ticker won't scroll. 我的新闻自动收录器不会滚动。 Can anyone help me out to fix it. 谁能帮我解决它。

I don't want to use html seperate. 我不想单独使用html。 I just want a complete javascript code. 我只想要一个完整的javascript代码。

var width = $('.ticker-text').width(),
  containerwidth = $('.ticker-container').width(),
  left = containerwidth;
$(document).ready(function(e) {
  function tick() {
    if (--left < -width) {
      left = containerwidth;
    }
    $(".ticker-text").css("margin-left", left + "px");
    setTimeout(tick, 16);
  } {
    var $markup = $('<div id="clientip"><div class = "ticker-container><div class = "ticker-text">start text text text text</div></div></div>');
    $markup.insertAfter('#header-userinfo');
  }
  tick();
});

CSS code: CSS代码:

#clientip {
    float: left;
    margin-right: 25px;
    margin-top: 12px;
    position: relative;
    vertical-align: middle;
}
.ticker-container {
    width: 100%;
    height: 2.9%;
    border: 1px solid;
    overflow: hidden;
}
.ticker-text {
    height: 150%;
    white-space:nowrap;
    display:inline-block;
}

You are trying to get the width of .ticker-text and .ticker-container at the beginning of your script, but insert them after that. 您试图在脚本的开头获取.ticker-text.ticker-container的宽度,但在此之后插入它们。 If you reorganize the code a little it should work. 如果您稍微重新组织代码,它应该可以工作。

$(document).ready(function(e){
    // insertAfter returns the jQuery object.
    var $markup = $('<div id="clientip"><div class = "ticker-container><div class = "ticker-text">start text text text text</div></div></div>').insertAfter('#header-userinfo'),
        $tickerText = $(".ticker-text"),
        width = $tickerText.width(),
        containerwidth = $('.ticker-container').width(),
        left = containerwidth;

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

    tick();
});

That's untested. 这未经测试。 I also created a variable for the ticker text dom object, to prevent querying the dom on each frame. 我还为代码文本dom对象创建了一个变量,以防止在每个框架上查询dom。

If you don't need anything special, maybe you can use <marquee> ( docs ) 如果您不需要任何特殊的东西,也许可以使用<marquee>docs

Your issue is that you are calculating the dimensions of your .ticker-container element before it exists! 您的问题是,您正在计算.ticker-container元素的尺寸!

So move the width variables down underneath the insertAfter() . 因此,将width变量向下insertAfter() 下面

You also have a couple of typos in the generated html ( class="ticker-container missing a quote, extra space between attributes). 在生成的html中,您也有一些错字( class="ticker-container缺少引号,属性之间有多余的空格)。

This should work better: 这应该更好地工作:

 $(document).ready(function(e) { function tick() { if (--left < -width) { left = containerwidth; } console.log(containerwidth); $(".ticker-text").css("margin-left", left + "px"); setTimeout(tick, 16); } var $markup = $('<div id="clientip"><div class="ticker-container"><div class="ticker-text">start text text text text</div></div></div>'); $markup.insertAfter('#header-userinfo'); // calculate dimensions here now the elements exist! var width = $('.ticker-text').width(), containerwidth = $('.ticker-container').width(), left = containerwidth; tick(); }); 
 #clientip { float:left; width:150px; margin-right: 25px; margin-top: 12px; position: relative; vertical-align: middle; } .ticker-container { width: 100%; height: 2.9%; border: 1px solid #000; overflow: hidden; } .ticker-text { height: 150%; white-space:nowrap; display:inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="header-userinfo"></div> 

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

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