Load the div first before delaying the load of the next element using jQuery

Hey there! I'm currently working on a gallery with left-floated divs, where each picture serves as the background for the div. What I'd like to achieve is having each picture load with a slight delay; meaning the first picture loads, then waits for about 5 seconds before the second one loads, and so forth in a sequential order. I have come across this feature on websites before, but unfortunately, I keep forgetting to bookmark it. If possible, I would like to implement this using jQuery.

I did try using jQuery, but I was only able to get all the images to fade in at once, whereas I want each div to fade in individually.

Here is some sample code:

<div id="parent">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>

The child elements will be set to fade in sequentially; the first element appears, followed by a 5-second delay, and then the next one fades in, and so on.

Thank you!

Answer №1

If you're looking to achieve a fade-in effect for elements with jQuery, you can try the following code:

$('#parent .child')
    .hide()
    .each(function(index){
        var _this = this;
        setTimeout( function(){ $(_this).fadeIn(); }, 5000*index );
    });

Check out the demo here.

Answer №2

My favorite response is from @Gaby also known as G. Petrioli, but I'll still share mine.

Check out the Live Demo

CSS

.child{display:none;}

JS

showElements($('#parent'));
    
function showElements(element){
    element.children('div').each(function(){
        if($(this).is(':hidden')){
            $(this).fadeIn();
            setTimeout(function(){showElements(element)}, 1000);  
            return false;
        }
    });
}

Answer №3

To start, hide all of your child elements by adding this CSS rule: .child {display:none;}

Next, use a recursive fade-in function like the one below:

function fadeInElements(element){
    $(element).fadeIn('slow', function(){
        if($(element).next().length > 0){
            setTimeout(function(){fadeInElements($(element).next());},5000);
        }
    });
}
fadeInElements($('.child:first-child'));

http://jsfiddle.net/X3J1F/4/

Answer №4

jQuery delay method

Here is an example:

<script>
    $("button").on("click", function() {
      $("div.first").slideUp(300).delay(800).fadeIn(400);
      $("div.second").slideUp(300).fadeIn(400);
    });
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving information from Flask server using an Ajax request

Exploring Flask and Ajax, my server-side web application is meant to double and return a passed number. I adapted the code from the example on Flask's site, resulting in this Python snippet: from flask import Flask, request, jsonify # Initialize the ...

Elements appear with varying widths in Firefox compared to Webkit-based browsers

I believe it would be simpler for you to inspect the elements rather than me writing the code, so I have provided a link: music band site. Now onto the issue: When the window width is reduced in Firefox, you will notice that the social icons in the yellow ...

iOS fixed positioning and scrolling

One of the current challenges we are facing involves implementing an action button similar to the one found in GMail for adding new content. This button needs to remain fixed during scroll. Here is the HTML code: <div class="addButton actionButton" on ...

Ajax search implementation presents challenges in Laravel development

I have been working on my own custom PHP application. I'm trying to add a search feature using AJAX, but my code doesn't seem to be functioning correctly. index.php <?php if (isset($_POST['search'])) { // Perform search fu ...

Using Css to Style an Image within Html.ActionLink

Html.ActionLink("", "ActionResult", new { CustomerId= DataBinder.Eval(c.DataItem, "CustomerId") }, new { target = "_blank", @style = "background-image:url(/Image/edit.png); width:50px; height:30px;" })); Hello there, I am facing an issue setting an image ...

How can you implement div slide animations using AngularJS?

Is there a way to slide a div when a user clicks on a tab? I have created a demo in jQuery Mobile (view demo here) with three tabs that show transitions. Can you provide guidance on how to achieve the same functionality in Angular? I have been attempting t ...

When Ajax responseText and echo fail, the header file contents are sent back instead

I have a section of code in my PHP file called thePhpFile.php that is used to handle an Ajax call: <?php require_once('usefulStuff.php'); // includes useful functions if (isset($_GET['zer'])) { $bFound = false; if(! $bF ...

Determine if the user's intention is to tap or to scroll up and down the page on a touch-sensitive device

When a user taps outside of a popup-div (touchstart), the popup should be hidden. However, if the user's intention is to scroll/swipe (touchmove), the popup should not be hidden. Can you provide a code example that can detect and handle these two ac ...

React-Router failing to properly unmount components when the location parameter changes

I'm struggling with a frustrating issue - I have a simple app created using create-react-app and I'm utilizing React-Router. JSX: <Router> <Route path="/pages/:pageId" component={Page} /> </Router> Within the Page ...

I have a page division with scroll bars and I want to ensure that the entire content of the division is displayed

I am facing an issue with a div and table on my webpage: <div id="tablediv"> <table id="table"> <tr><th>headers</th></tr> <tr><td>contents</td></tr> </table> <d ...

Tips for submitting form data with JQuery AJAX

Trying to figure out how to handle a form that uses a multi-select plugin The challenge is sending the multiple select dropdown values stored in a variable to PHP along with the rest of the form data. Below is the structure of the form: <d ...

Refresh the mapbox source features in real-time

Currently, I am mapping out orders on a map with layers and symbols that have different statuses. When the status of an order changes, I want to update the color of the symbol accordingly. The layer configuration looks like this: map.addLayer({ id: &q ...

Set the return value of the mongoose function to a variable in node.js

As a newcomer to node js and mongoose, I have encountered the following code in my node js project. I am trying to store the mongoose return record userData in a variable user that is outside of the callback function. var user = null; User.findOne({$and: ...

Troubleshooting Issues with Integrating Bootstrap Carousel

UPDATE: When I attempt to click on either the left or right icons, it unexpectedly scrolls me down to the bottom of the page. It seems to be related to the #truespeed aspect. The carousel functions perfectly when it is on a standalone page but causes issue ...

Having trouble showing an image with jQuery after it has been uploaded

Currently, I have a URL that shows an image upon loading. However, I want to provide the option for users to replace this image using a form input. My goal is to display the uploaded image as soon as it's selected so users can evaluate it. I'm a ...

What is the maximum number of data points that can be used in a Scatter plot created

I am currently facing an issue while trying to populate a Scatter plot in JavaScript with decimal values obtained from a MySQL database using PHP. The database contains around 150,000 entries, resulting in 150,000 pairs of decimal inputs for the graph. How ...

The Firebase Auth Multifactor feature does not include the multiFactor property in the user object

Looking to implement Multi-Factor Authentication (MFA) in my web app, but encountering an issue where the multiFactor property is missing in the code snippet below: import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.2/firebase-app.j ...

"Utilizing AngulaJS to apply a class to the parent element when a radio button is

I'm wondering how I can assign a class to the parent div of each radio button in a group? You can find the code below and view the corresponding JSFiddle here. Here is the HTML: <div class="bd"> <input type="radio" ng-model="value" val ...

The art of organizing information: Tables and data management

Looking for a solution with a table: <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </table> Trying to format the cells to display as: 1 2 3 4 ...

AngularJS Bootstrap Datepicker populating date from input field

I'm currently utilizing Bootstrap for my web application. I've developed a method that initializes all input fields of type date with a class called form_datetime: function initDatepicker(){ $(".form_datetime").datepicker({ ...