Skip to content Skip to sidebar Skip to footer

Jquery Fadein Children Elements Of Different Element Types Sequentially

I'm trying to fadeIn() the children elements of a parent element using jQuery. The children elements are all of different types. For example one of my parent elements may look like

Solution 1:

Callbacks and recursion? Bah humbug! :-)

You can also try this simpler, non-callback solution:

var elems = $('.section-content-parent').children();

$(elems).each(function(index) {
    $(this).delay(1200*index).fadeIn(1000);
});

See it in action: https://jsfiddle.net/fppjkv0o/30/

Source: https://stackoverflow.com/a/4661858/1152633

Make it reusable for other parent elements:

functionfadeInChildren(parent) {
    var elems = $(parent).children();

    $(elems).each(function(index) {
        $(this).delay(1200*index).fadeIn(1000);
    });
}

And use the function like this:

fadeInChildren('.section-content-parent');

Solution 2:

To make them fade in after each other we can use the callback method on fade in.

First, add this function so we can call it recursively

functionfadeChildrenIn(childNum){

    var children = $("#section-content-parent").children();

    var $child = $(children[childNum]);
    $child.fadeIn(function(){
        if(childNum < children.length)
            fadeChildrenIn(++childNum);
    });
}

And then when you want the event to happen you can use

fadeChildrenIn(0);

The argument is the index of the child we are fading, the function then checks if the index is less than the total children, if it is when the current child has faded it moves onto the next one, if there are no more children left is simply stops.

This will also fulfill your requirement of working with any type of child element, IE. span, img etc.

Post a Comment for "Jquery Fadein Children Elements Of Different Element Types Sequentially"