2

我找到了这个答案:

如何在使用 jQuery 之后更改文本?

这基本上可以满足我的需要。但不是让它在最后重定向到另一个页面,我希望它只是不断地循环浏览短信。显然,我删除了 IF 部分以停止重定向,但是如何让它反复循环浏览短信?

function nextMsg() {
    if (messages.length == 0) {
        // once there is no more message, do whatever you want
        alert("redirecting");
    } else {
        // change content of message, fade in, wait, fade out and
        // continue with next message
        $('#message').html(messages.pop()).fadeIn(500).delay(1000)
                                          .fadeOut(500, nextMsg);
    }
};

// list of messages to display var messages = [
//   "Hello!",
//   "This is a website!",
//   "You are now going to be redirected.",
//   "Are you ready?",
//   "You're now being redirected..."
// ].reverse();

// initially hide the message $('#message').hide();

// start animation nextMsg();
4

2 回答 2

5

只需对基本代码进行一些小改动:

function nextMsg(i) {
    if (messages.length == i) {
        i = 0;
    }

    $('#message').html(messages[i])
                 .fadeIn(500)
                 .delay(1000)
                 .fadeOut(500, function() {
                     nextMsg(i + 1);
                 });
};

nextMsg(0);​

演示:http: //jsfiddle.net/8wqv2/

于 2012-09-24T20:25:26.823 回答
0

这样的东西会起作用吗?

于 2012-09-24T20:28:44.770 回答