Fader Ticker JQuery和PHP

时间:2011-05-14 13:29:57

标签: php jquery ticker

我写的这个(可爱)小脚本应该轻轻淡出列表<li></li>中的每个元素。但没有任何事情发生。怎么了。

<div id="twitnews" style="padding-left:20px; line-height:20px; float:left;">
        <?php getFeed("http://search.twitter.com/search.rss?q=golf+berkshire"); ?>
    </div>

<script type="text/javascript">
function fader() {

$(element).fadeIn(300, function(){
    $(element).delay(3000).fadeOut(300) });

        var element = $(element).next();

        fader();

}
$('#twitnews').children().hide();

var element = $('#twitnews').children().first();
fader();
        </script>

任何想法,

非凡

4 个答案:

答案 0 :(得分:0)

原始代码中存在许多错误,选择器不正确,全局变量未正确使用,这非常适合插件演示。

(function($){
// Make it a plugin
$.fn.twicker = function(settings) {
  // Default Variables
  var c = {time: 1300};
  // Override the variables with the settings if available.
  $.extend(c, settings);
  var $t = $(this);
  // If we find a ul, means we passed the container (a div that contains a ul)
  if($('ul', $t).length == 1) {
      $tw = $t;
      $t = $('li', $t).first();
  }

  // Do the animation.
  $t.delay(c.time).fadeIn(c.time, function() {
    // If there's a next sibling, do next.
    if($t.next().length != 0) {
        $t.next().twicker();
        $t.next().fadeOut();

    // If there's no sibling, it means we're at the end. Go back to first.
    } else {
        $tw.first().twicker();
        $t.first().fadeOut();
    }
    $t.fadeOut(c.time);
  });

};

})(jQuery);

$('#twitnews').twicker({time:2000});

您可以在JSFiddle

上实时查看

答案 1 :(得分:-1)

尝试将element传递给fader()

fader(element);

在脚本的末尾。

定义element

时,还要包含fader()
function fader(element){
    //the rest of your function code here
}

答案 2 :(得分:-1)

我不知道为什么你的脚本不起作用,但我建议你采取一些不同的方法。用以下代码替换所有JS代码:

$('#twitnews').children().each(function(){
  $(this).fadeIn(300, function() {
    $(this).delay(3000).fadeOut(300);
  });
});

注意:我只重新排列了代码以摆脱全局变量,奇怪的元素遍历和递归调用。我没有触摸动画 - 但你应该能够使用新代码轻松找到错误。

答案 3 :(得分:-1)

我认为这是一个非常简单的解决方案。唯一的问题是使用if函数来确定这是否是最后一个li不起作用。也许有人知道它有什么问题。

 $('#twitnews li').hide();
    $('#twitnews li').first().fadeIn(300, function fade(){

        var visel = $('#twitnews li:visible');

        if((visel).is($('#twitnews li:last'))) {
            var nextel =  $('#twitnews li').first();
        }
        else {
        var nextel = $(visel).next();
        }


        $(visel).delay(3000).fadeOut(300, function(){

            $(nextel).fadeIn(300, function(){fade()});

            });

        });