0

这段代码出了点问题,但已经晚了,我就是想不通:

var json_object = '.$json_twitter_array.';

function displayTweets(obj){
    $("#tweetblock").html(obj[0].description).fadeIn("slow");
    var i = 1;
    setInterval(
        function(){
            $("#tweetblock").html(obj[i].description).fadeIn("slow");
            i++;
            if(i >= obj.length) i = 0;
        },6000);
}
displayTweets(json_object);

它循环通过 json 对象就好了,问题是淡入淡出不起作用,只是显示了文本。有人可以指出我哪里出错/错过了显而易见的事情吗?

非常感谢!

4

3 回答 3

3

问题是您在实际放置内容后使对象褪色。解决此问题的一种快速方法是hide()在发生之前为每个元素添加一个html(),然后将内容添加到隐藏区域,然后再fadeIn()启动。

所以每个:

$("#tweetblock").html

变成:

$("#tweetblock").hide().html

最终代码应如下所示:

var json_object = '.$json_twitter_array.';

function displayTweets(obj){
    $("#tweetblock").hide().html(obj[0].description).fadeIn("slow");
    var i = 1;
    setInterval(
        function(){
            $("#tweetblock").hide().html(obj[i].description).fadeIn("slow");
            i++;
            if(i >= obj.length) i = 0;
        },6000);
}
displayTweets(json_object);
于 2013-02-01T19:45:15.757 回答
1

您需要先将其淡出,然后才能再次淡入!

fadeIn并且fadeOut只改变元素的可见性。当内容发生变化时,他们不会做任何事情。

于 2013-02-01T19:44:01.030 回答
1

只需在 css 中做一件事:

#tweetblock{
   display:none;
}

然后fadeIn()就会发生。

于 2013-02-01T19:50:16.327 回答