jQuery looping .fadeIn and .fadeOut of p tags within div one at a time

*爱你&永不变心* 提交于 2019-12-01 00:17:20

This is a very simple solution:

function fade($ele) {
    $ele.fadeIn(6000).delay(3000).fadeOut(6000, function() {
        var $next = $(this).next('p');
        // if there is a following `p` element, it is faded in
        // otherwise start from the beginning by taking
        // the parent's first child
        fade($next.length > 0 ? $next : $(this).parent().children().first());
   });
};  

fade($('#tml-container > p').first());

DEMO


Reusable plugin version:

Instead of iterating over the children of a certain element, it iterates over the selected elements.

(function($) {
    var default_config = {
        fadeIn: 3000,
        stay: 3000,
        fadeOut: 3000
    };

    function fade(index, $elements, config) {
        $elements.eq(index)
          .fadeIn(config.fadeIn)
          .delay(config.stay)
          .fadeOut(config.fadeOut, function() {
              fade((index + 1) % $elements.length, $elements, config);
          });
    }

    $.fn.fadeLoop = function(config) {     
        fade(0, this, $.extend({}, default_config, config));
        return this;
    };

}(jQuery));

Usable as:

$('#tml-container > p').fadeLoop({fadeIn: 6000, stay: 3000, fadeOut: 6000});

DEMO

Changed a self developed Plugin to meet your requirements, didn't test it fully though.

Should at least show you how to set something like this up properly.

Hope this helps :)

(function($){
    $(function(){
        $('#tml-container').testimonialFader();
    });

    $.fn.testimonialFader = function(){
        return this.each(function(){
            var $par = $(this),
            $testimonials = $par.children('p'),
            $current = $testimonials.first();

            $testimonials.not(':first').hide();

            $testimonials.on('testimonialReady.testimonialFader', wait);
            $testimonials.on('waitComplete.testimonialFader', showNext);

            $current.trigger('testimonialReady.testimonialFader');

            function wait(e){
                $current.trigger('waitStart.testimonialFader');
                setTimeout(
                    function(){
                            $current.trigger('waitComplete.testimonialFader');
                        },
                        6000
                );
            }
            function showNext(){
                $current.trigger('testimonialChange.testimonialFader');
                if($testimonials.length > 1){
                    var $next = $current.next();
                    if($next.length == 0){
                        $next = $testimonials.first();
                    }
                    $current.fadeOut(800);
                    $next.fadeIn(800, 
                        function(){
                            $next.trigger('testimonialReady.testimonialFader');
                        }
                    );
                    $current = $next;
                }
            }

        });
    };
}(jQuery));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!