jQuery: Find word and change every few seconds

后端 未结 6 1212
[愿得一人]
[愿得一人] 2021-01-03 00:12

How can I change a word every 2-3 seconds using jQuery?

For example:

I have this:

This is so <
6条回答
  •  感动是毒
    2021-01-03 01:17

    This should work. First, add an id to the span that you want to rotate the text on. E.g.,

    awesome
    

    And in your JavaScript:

    $(function() {
        var words = ['cool', 'fantastic', 'incredible', 'awesome'],
            index = 0,
            $el = $('#rotate-word')
        setInterval(function() {
            index++ < words.length - 1 || (index = 0);
            $el.fadeOut(function() {
                $el.text(words[index]).fadeIn();
            });
        }, 3000);
    });
    

    You can see this in action here: http://jsfiddle.net/DMeEk/

提交回复
热议问题