How to make this jQuery animation code loop forever?

前端 未结 3 993
清歌不尽
清歌不尽 2021-01-06 04:18

i am trying to create a loop of text animations over a slider... i tried loop but it didnt work.. Can you please tell me how do i loop this script forever..thanks

         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-06 04:45

    If you clean your code a bit. You can realize that setInterval is not needed. Simply make use of the last callback to repeat the animation.

    $(function () {
        var $header = $("#header");
        var header = ['I', 'Am', 'So', 'Cool'];
        var position = -1;
    
        !function loop() {
            position = (position + 1) % header.length;
            $header
                .html(header[position])
                .fadeIn(1000)
                .delay(1000)
                .fadeOut(1000, loop);
        }();
    });
    

    See it here.

提交回复
热议问题