1

我写的这个(可爱的)小脚本应该轻轻地淡出 list 中的每个元素<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

4 回答 4

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上实时查看

于 2011-05-14T15:00:42.487 回答
-1

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

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

注意:我只是重新排列了您的代码以摆脱全局变量、奇怪的元素遍历和递归调用。我没有接触动画——但你应该能够使用新代码很容易地找到错误。

于 2011-05-14T13:49:42.530 回答
-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()});

            });

        });
于 2011-05-14T15:06:16.587 回答
-1

尝试传递elementfader().

fader(element);

在脚本的末尾。

element定义时还包括fader()

function fader(element){
    //the rest of your function code here
}
于 2011-05-14T13:40:42.197 回答