The never-ending delay of an infinite fade in and out CSS3 animation

I am currently working on the following:

Fiddle Code

This is the HTML structure being used:

<div id="animation">
  <ul>
    <li>this is</li>
    <li>CSS3 looped</li>
    <li>animation</li>
  </ul>
</div>

Here is the CSS for the animation:

#animation {
    height: 100%;
    width: 100%;
}
#animation ul {
    position: relative;
    text-align: center;
    width: 100%;
}
#animation li {
    position: absolute;
    left:0;
    top:0;
    width: 100%;
    opacity: 0;
    padding: 10px;
}
#animation li:nth-of-type(1) {
    -webkit-animation: fadein 6s ease-in-out -4s infinite alternate;
    -moz-animation: fadein 6s ease-in-out -4s infinite alternate;
    animation:fadein 6s ease-in-out -4s infinite alternate;
}
#animation li:nth-of-type(2) {
    -webkit-animation: fadein 6s ease-in-out 0s infinite alternate;
    -moz-animation: fadein 6s ease-in-out 0s infinite alternate;
    animation: fadein 6s ease-in-out 0s infinite alternate;
}
#animation li:nth-of-type(3) {
    -webkit-animation: fadein 6s ease-in-out 4s infinite alternate;
    -moz-animation: fadein 6s ease-in-out 4s infinite alternate;
    animation: fadein 6s ease-in-out 4s infinite alternate;
}
@-webkit-keyframes fadein {
    0% {
        opacity: 0;
    }
    66% {
        opacity: 0;
    }
    76% {
        opacity: 1;
    }
    100% {
        opacity: 1;
    }
}
@-moz-keyframes fadein {
    0% {
        opacity: 0;
    }
    66% {
        opacity: 0;
    }
    76% {
        opacity: 1;
    }
    100% {
        opacity: 1;
    }
}
@keyframes fadein {
    0% {
        opacity: 0;
    }
    66% {
        opacity: 0;
    }
    76% {
        opacity: 1;
    }
    100% {
        opacity: 1;
    }
}

As I am relatively new to CSS3, my goal is to have paragraphs instead of single words fadeIn and stay on the screen for a certain amount of time before fading out into the next paragraph. Unfortunately, using duration and delay properties didn't give me the desired result. Any insights or help on achieving this effect would be greatly appreciated.

Answer №1

The concept is quite straightforward, requiring some basic mathematical calculations as suggested in the comment by Paulie_D. The decision of whether or not to implement this technique is up to you. Personally, I find no issues with this method nor any complications, especially if the number of elements being faded in/out remains constant.

Here is an overview of the approach:

  • We have 3 elements/paragraphs; for demonstration purposes, let's assume they fade in over the first 3 seconds, remain static for the next 10 seconds, and then fade out at the end. Therefore, each element will require a total of 16 seconds for the animation process.
  • While one element completes its animation and another begins, the previous elements should maintain their final state (faded out). To achieve this, the following steps need to be taken:

    • Set the animation-duration for all elements to the combined animation time of each element. In this case, it would be 3*16s = 48s.
    • Define the keyframes so that each element remains idle for 32s of the total duration, allowing other elements to undergo their animations. This is accomplished by ensuring that the fade-in, stay, and fade-out occur within the initial 33% of the total animation duration.
    • Assign an animation-delay of 16s to the second element (to start after the completion of the first) and 32s for the third element (after the first two complete).
  • Regarding the keyframes rule itself, as mentioned earlier, the entire animation for one element must finish within 33% of the full duration. At 6.25% (approximately 3s mark), we initiate the fade-in, maintain opacity: 1 until 26.75% (around 13s mark), and finally fade out completely at 33% (at the 16s mark).

#animation {
  height: 100%;
  width: 100%;
}
#animation ul {
  position: relative;
  text-align: center;
  width: 100%;
}
#animation li {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  opacity: 0;
  padding: 10px;
}
#animation li:nth-of-type(1) {
  animation: fadein 48s ease-in-out infinite;
}
#animation li:nth-of-type(2) {
  animation: fadein 48s ease-in-out 16s infinite;
}
#animation li:nth-of-type(3) {
  animation: fadein 48s ease-in-out 32s infinite;
}
@keyframes fadein {
  0% {
    opacity: 0;
  }
  6.25% { /* 3s for fade in */
    opacity: 1;
  }
  26.75% { /* roughly 10s for stay as-is */
    opacity: 1;
  }
  33% { /* 3s for fade out */
    opacity: 0;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div id="animation">
  <ul>
    <li>This is</li>
    <li>CSS3 looped</li>
    <li>animation</li>
  </ul>
</div>

Answer №2

Here is the basic CSS code for this example:

.visible {
  visibility: visible;
  opacity: 1;
  transition: opacity 2s linear;
}
.hidden {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 2s, opacity 2s linear;
}

When displaying the element (using the visible class), we want the visibility to change immediately but transition only the opacity property. When hiding the element (using the hidden class), we delay the visibility:hidden declaration for a fade-out effect. This is achieved by applying a transition on the visibility property with a duration of 0s and a delay.

After the fade-out transition, we aim to remove the hidden element from the layout to avoid leaving any gaps. Unfortunately, options are limited:

  • The display:none property applies instantly;

  • Using position:absolute has a similar issue;

  • We can resort to using margin-top as a workaround since it can be transitioned and delayed.

To utilize margin-top for hiding the element, a slightly more elaborate HTML structure is required:

<div class="visible">
  <div>…</div>
</div>

This results in a more complex CSS code:

.visible,
.hidden {
  overflow: hidden;
}
.visible {
  visibility: visible;
  opacity: 1;
  transition: opacity 2s linear;
}
.hidden {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 2s, opacity 2s linear;
}
.visible > div,
.hidden > div {
  /* Add padding, borders, min-height, etc. here. */
}
.hidden > div {
  margin-top: -10000px;
  transition: margin-top 0s 2s;
}

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

Is it possible to trigger a script tag based on certain conditions using a JavaScript function?

I'm currently working with Angular and have a requirement to trigger a third party script from a function located within another script tag. Here's an example scenario: Within the index.html file let displayOnHomepage = () => { if (win ...

Button click not functioning to switch the displayed image in Javascript

As a beginner in Javascript, I'm attempting to create a basic button in html that switches the originally shown image when clicked. Unfortunately, my current code isn't functioning properly. Could someone please assist me in identifying the mista ...

"Getting Started with Respond.js: A Step-by-Step

I've been struggling to find clear instructions on how to properly set up respond.js. Should I just unzip it into the htdocs folder, or do I only need respond.min.js in there? Then, do I simply reference the file like this... <script src="respon ...

What is preventing me from choosing the complete table?

I am currently working on a method to export tabular data from an HTML page to Excel using DocRaptor. My approach involves using PHP to pass the necessary data through their API for conversion into an Excel Spreadsheet. However, I have encountered an issu ...

The functionality of CKEditor is compromised when used in conjunction with React Material-UI

I've been struggling with integrating Material UI and CKEditor5. The issue I'm facing is that CKEditor doesn't seem to be working properly. Despite trying to apply editor features like bold or italic, nothing changes on the screen. However, ...

Map will be visible correctly only when the window is resized

I'm currently working on a project that utilizes a map engine from a private company which I am unable to disclose. The initial system was built in JQuery, but we recently underwent significant changes and now have it running on AngularJS. One side ...

Update the color of a span in JQuery when its value equals 0

Currently, my goal is to update the color of a span tag only if its value is 0. I attempted to achieve this with the following code: $('span.number:contains("0")').css('color', '#C1C1C1'); However, this code also changes the ...

jQuery live DataAttribute manipulation

Here is the code snippet I am working with: <head runat="server"> <title>Sidebar</title> <link href="css/style.css" rel="stylesheet" /> <script src="scripts/jquery-1.11.1.min.js"></script> <script src ...

What is the best method for converting a variable with HTML content into a printable string?

In an attempt to display user-entered data from a text box to an HTML div, there seems to be an issue when the data contains HTML content. Instead of displaying the content as a string, it shows it as HTML elements. For example: let text = "<h1>Worl ...

Updating class after refresh of ng-repeat in AngularJS/Javascript

I am currently using angularJs in conjunction with cordova. Within my ng-repeat loop, each item has an ng-click event that stores the value of the item in an array. Additionally, when I click on an item, it toggles a class on the corresponding div (using ...

The table borders in IE 11 do not display properly when using Html5 and CSS

In a previous version, the table had visible borders. However, when I added the property text-align:center; to my container, the table did not align to the center. I then tried adding display:inline-block; to the table, which successfully centered it. It l ...

Creating a responsive design using HTML and CSS to ensure that all elements fit perfectly on any window size or resolution

Currently, my friend and I are collaborating on a mod for the popular game known as Minecraft. My responsibility is to create a website where we can showcase this mod and provide information about its features. The main issue I am encountering is the inab ...

The method of stamping masonry is not encircling

I recently discovered a new method in Masonry 3 called "stamp" that allows you to fix an element in place. However, I am finding that it is not working as expected. Check out this example from David DeSandro: http://codepen.io/desandro/pen/wKpjs Initial ...

Using a slider in Cordova application

Currently, I am in the process of developing an app using Cordova. However, I have encountered an issue with the slider feature. HTML <body> <div id="slider"> <div id="custom-handle" class="ui-slider-handle"></div> </di ...

Why isn't the main body of CSS extending across the entire page?

I feel like I'm running headfirst into a wall trying to figure out why the code I wrote for this specific website isn't quite working properly. The main content area of my pages (the white space in the link below) is supposed to extend from the ...

Carousel-Owl, glide through two items simultaneously

I am currently in the process of developing a slider using the beta version of Owl-Carousel 2, but I am encountering several issues. My goal is to configure the owlCarousel to function as follows: It should scroll through 2 items at a time while displayin ...

Change the background color of the top element box when scrolling

The code I have implemented reflects my intentions. html: <div id="one" class="box"></div> <div id="two" class="box"></div> <div id="thr" class="box"></div> <div id="fou" class="box"></div> <div id="fiv" ...

Is there a way to trigger the animation of this text effect only when the mouse is scrolled to that specific section?

Here is a cool typing text effect created using HTML, CSS, and JavaScript. Check out the code below: (function($) { var s, spanizeLetters = { settings: { letters: $('.js-spanize'), }, init: function() { ...

Tips for deleting all content within a designated html tag (including the tag itself)

Imagine you are dealing with a string like this: text = """<p>Bla bla bla.</p><p>Blo blo blo<a href="http://www.example.com">bli bli</a>.</p><p>blu blu<br> <span style="font-size: x-small;"><br> ...

Dynamic styling with jQuery input focus animation

Whenever I interact with the input field, I want my border colors to animate in and fade out smoothly. But I'm facing some challenges. Here is how I've set it up: HTML <input type="text" id="search-input" placeholder=&quo ...