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

前端 未结 2 1385
甜味超标
甜味超标 2021-01-06 14:37

The code below successfully fades in one testimonial for 6 seconds, waits 3 seconds, and then fades it out and moves on to the next. Once it reaches the third testimonial it

相关标签:
2条回答
  • 2021-01-06 14:47

    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));
    
    0 讨论(0)
  • 2021-01-06 14:53

    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

    0 讨论(0)
提交回复
热议问题