简体   繁体   English

如何显示/隐藏生成的无序列表

[英]How to show/hide an unordered list that is generated

I am using this code to generate the last 5 tweets from a given user. 我正在使用代码从给定用户生成最后5条推文。

What I am trying to do is to show each tweet one at a time. 我想做的是一次显示每条推文。

I've modified the script so that the generated <ul> has an id. 我已经修改了脚本,以便生成的<ul>具有ID。 You can see the change I made on line 6 of the script. 您可以在脚本的第6行看到我所做的更改。

Here is the script I'm using: 这是我正在使用的脚本:

$(document).ready(function () {
var tweets = $('#tweetlist li').hide();
    i = 0;

(function cycle() { 

    tweets.eq(i).fadeIn(600)
              .delay(4000)
              .fadeOut(600, cycle);

    i = ++i % tweets.length;

})();
});

For some reason it just doesn't work. 由于某种原因,它根本不起作用。 I have jquery called before both scripts, but the list still shows all of the items. 我在两个脚本之前都调用了jquery,但是列表仍然显示所有项目。

Here is the jsfiddle I've been using: http://jsfiddle.net/wulfmann/qst8atkk/1/ 这是我一直在使用的jsfiddle: http : //jsfiddle.net/wulfmann/qst8atkk/1/

The weird thing is that if I build the list myself it works. 奇怪的是,如果我自己建立列表,它就可以工作。

I have a feeling that I'm missing something fundamental. 我觉得我缺少一些基本的东西。 Admittedly I have not gone through the twitter script code line by line, so I may have missed something there. 诚然,我还没有逐行浏览Twitter脚本代码,因此我可能错过了一些东西。

I am learning jquery and javascript now so I'd just like to make sure I'm not missing something basic. 我现在正在学习jquery和javascript,所以我只想确保我没有错过一些基本的知识。

edit: In the attached jsfiddle I've unminified the js so that you can see. 编辑:在所附的jsfiddle中,我将js缩小了,以便您可以看到。

Your fadeIn / fadeOut loop is fine. 您的fadeIn / fadeOut循环很好。 However, in your example you run it after documents load, and before the async action (fetching the tweets) ends, which means the tweets list is empty, and afterwards the full list is rendered. 但是,在您的示例中,您是在文档加载之后,异步操作(获取推文)结束之前运行它的,这意味着推文列表为空,然后呈现完整列表。

To prevent that use the customCallback options in the config. 为了防止这种情况, customCallback在配置中使用customCallback选项。 After the items are fetched, the callback will render them, hide them, and then execute the fadeIn / fadeOut loop ( demo ): 提取项目后,回调函数将渲染它们,将其隐藏,然后执行fadeIn / fadeOut循环( demo ):

var configProfile = {
  "profile": {"screenName": 'twitter'},
  "maxTweets": 5,
  "enableLinks": true, 
  "showUser": true,
  "showTime": true,
  "showImages": false,
  "customCallback": handleTweets,
  "lang": 'en'
};
twitterFetcher.fetch(configProfile);

function handleTweets(tweets) {
    var x = tweets.length;
    var n = 0;
    var element = document.getElementById('tweets');
    var html = '<ul>';
    while(n < x) {
      html += '<li>' + tweets[n] + '</li>';
      n++;
    }
    html += '</ul>';
    element.innerHTML = html;

    var tweets = $(element).find('li').hide();

    var i = 0;

    (function cycle() { 

      tweets.eq(i).fadeIn(600)
        .delay(4000)
        .fadeOut(600, cycle);

      i = ++i % tweets.length;

    })();
}

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

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