Creating a looping animation on multiple elements using jQuery that makes them fade in and out at different intervals

I am currently working on creating a fade in and out animation for multiple elements using jquery. It's a bit complex to explain, so I will start by sharing the relevant code.

$(document).ready(function() {
      $("#1").delay(0).animate({
        'opacity': '1'
      }, 1000);
      $("#2").delay(1000).animate({
        'opacity': '1'
      }, 1000);
      $("#3").delay(2000).animate({
        'opacity': '1'
      }, 1000);
      $("#4").delay(3000).animate({
        'opacity': '1'
      }, 1000);
    });
.hideme {
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div id="1" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
  <div id="2" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
  <div id="3" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
  <div id="4" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
</div>

The current functionality of this code involves each of the four parent divs within the container fading in one by one. What I aim to achieve is to then have all the divs with the class "first" fade out simultaneously, followed by each of those "first" divs fading in sequentially (similar to the initial loading sequence but only affecting the "first" divs), then having them fade out again together, and repeating this process indefinitely. Essentially, the "first" divs will be alternating between fading in and out, while the "second" divs will remain visible continuously after their initial fade-in. It is crucial that the "first" divs do not fade in at the same time, but rather one after the other. I am encountering challenges in coding this specific requirement.

Answer №1

Using CSS instead of jQuery is sufficient for this task. Make sure "first" and "second" are classes, not IDs, as IDs need to be unique.

.second {
  opacity: 1;
  animation-name: fadeInOpacity;
  animation-iteration-count: 1;
  animation-timing-function: ease-in;
  animation-duration: 1s;
}

.first {
  opacity: 1;
  animation-name: fadeInOpacity;
  animation-iteration-count: infinite;
  animation-timing-function: ease-in;
  animation-duration: 1s;
}

@keyframes fadeInOpacity {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<div class="container">
  <div id="1" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
  <div id="2" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
  <div id="3" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
  <div id="4" class="hideme">
    <div class="first">A</div>
    <div class="second">B</div>
  </div>
</div>

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

Using the max-width property with Semantic UI Dropdown in ReactJS

I'm struggling to determine how to adjust the max-width of the Dropdown element in ReactJS. I attempted the following: .Menu Dropdown { max-width: 5rem !important; } Unfortunately, this did not work as expected. The dropdowns are taking up too m ...

How can I prevent users from inputting negative numbers in a cellEditable material-table?

In my project, I am utilizing a material table with editable cells. I need to implement a restriction that prevents users from entering negative numbers in the field and displays an error validation message. How can I achieve this functionality? Check out ...

Incorporating HTML5 & CSS3: Leveraging various Pseudo classes and the translateX/Y properties

As a graphic designer dabbling in HTML5, I have to admit that coding is not my strong suit. Despite that, I've been able to troubleshoot most of my issues thus far. However, I've hit a bit of a snag. The problem arises when clicking on a thumbna ...

Top Margin: Supported by Chrome and Safari

After troubleshooting why the margin-top is not working on Safari, but works fine on Chrome, I discovered a discrepancy. Chrome Upon hovering over an item in the image, the cursor changes as expected. This feature operates smoothly in Chrome. https://i. ...

BeginForm in MVC: When Data Doesn't Get Posted

I'm having an issue with my MVC BeginForm code. I can't seem to access the value of input controls in my controller. Is there something wrong with my setup? using (Ajax.BeginForm("CreateApp", "App", new AjaxOptions { UpdateTargetId = "my-modal-d ...

JSON only retrieve the corresponding data

I am looking to send a JSON object back to Postman without including a "title" like: { "name": { "name": "Three Rivers Campground", "lengthLimit": 25, "elevation": 6332, ...

Are there alternative approaches to launching processes aside from the functions found in child_process?

Having trouble with NodeJS child_process in my specific use case. Are there other methods available to start processes from a node.js application? So far, Google has only been pointing me towards child_process for all of my inquiries. Edit I am looking to ...

Locking Bootstrap 4.3 Navbar at the Page's Summit with a See-Through Effect

I have extensively researched and searched online for information before reaching out here. I hope my question is not redundant. My challenge lies in creating a static navbar at the top of my Bootstrap 4.3 page. Despite multiple attempts, it seems to elud ...

Open the PDF document in a stylish jQuery popup like fancybox or a similar plugin

Can a PDF file be loaded in a popup? I understand that some browsers may require Adobe Reader to view PDFs, but Chrome has its own built-in PDF viewer. ...

HTML 5 Video VTT File: A must-have for optimal video playback

Having trouble getting the subtitle.vtt file to load on Chrome with this code. Can anyone offer any insights? <div id="videoKilledTheRadioStar"> <video id="Tutorial" width="420" autoplay controls> <source src="videos/shaco.mp4" type="vi ...

What could be causing my Bootstrap datepicker to malfunction?

A while ago, my Bootstrap datetimepicker component was functioning perfectly in my code. However, it has suddenly stopped working and I am seeking assistance to get it running smoothly again. Below is the HTML code snippet: <script src="https://cd ...

Ways to extract information from JSON files

Currently, I am working on a script to extract viewer count and follower count data from Twitch. While I have successfully retrieved the viewer count information, I am encountering issues with extracting the follower count. The essential information can be ...

Error: JSON at position 1 is throwing off the syntax in EXPRESS due to an unexpected token "

I'm currently utilizing a REST web service within Express and I am looking to retrieve an object that includes the specified hours. var express = require('express'); var router = express.Router(); /* GET home page. ...

How do I delete an element from an array in a MongoDB database using Node.js?

When running this query in ROBO 3T, the code executes correctly. However, when attempting to run it in nodejs, the code is not functioning as expected. //schema model const mongoose = require('mongoose'); const imageSchema = mongoose.Schema({ ...

What causes the translation of text within HTML tags to occur when attempting to retrieve it during Web Scraping?

Exploring the world of web scraping, I am currently immersed in a small project. In this snippet of code, I am capturing the HTML content and storing it in a variable named soup. source=requests.get(URL) soup=BeautifulSoup(source.text,'html.parser&apo ...

jquery function context

I'm having trouble grasping function scope in this scenario. When a button is clicked, it triggers a dialog box with a textarea inside displaying a URL that can be copied for camera setup. <button id="axis-details" onclick="apikey('<?php e ...

Stacking issue - elements not properly aligned

Is there a way to move my H1 text below the blue category button instead of beside it? Thank you in advance! http://jsfiddle.net/TDBzN/ <div id="article" class="animated2 fadeInLeftBig"> <div class="article-close"></div> <div c ...

Anchor tag buttons do not affect the font color and variables remain unchanged

I've been struggling to change the font color and background color of the <a> element, but nothing seems to work. I've attempted using hexadecimal notation and styling the .btn class without pseudo-classes, with no luck. Check out my CodeP ...

Can I switch between different accounts on Auth0?

While utilizing next-auth for user sign-ins, I've noticed that there isn't a clearly visible option to switch accounts after signing in. Additionally, if users enter incorrect credentials, there doesn't seem to be a way to sign in with a dif ...

How can I ensure the Jquery datepicker functions correctly?

I've been attempting to create a jsp page with some Jquery functionalities. Unfortunately, despite my best efforts, I am unable to make it work. I have downloaded jquery1.7.1 and jquery-ui1.8.17 (non-mini), renamed them to jquery171.js and jquery-ui. ...