Endless looping of numerous consecutive animations

Is it possible to apply two animations to an element using only CSS and without the need for JavaScript? These animations should run sequentially an infinite number of times.

@keyframes show {
  from,
  to {
    z-index: 100;
  }
}

@keyframes wait {
  from,
  to {
  }
}

.block {
  position: absolute;
  z-index: -100;
  width: 100px;
  height: 100px;
  border: 1px solid black;
  animation: 1s show infinite, 1s wait infinite;
}

.block-a {
    background-color: red;
    animation-delay: 0s, 1s;
}

.block-b {
    background-color: purple;
    animation-delay: 1s, 2s;
}

.block-c {
    background-color: yellow; 
    animation-delay: 2s, 3s;
}
<div class="container">
  <div class="block block-a">1</div>
  <div class="block block-b">2</div>
  <div class="block block-c">3</div>
</div>    

If you'd like to see my implementation, check it out on codepen: https://codepen.io/olafvolafka/pen/oNpPeqj

The one issue I'm facing is that the animation stops after the last sequence instead of repeating as desired.

Answer №1

Displayed below is the implementation of A Note from Haworth to ensure that the z-index returns to its initial value (-100) at the end of each animation iteration.

This is achieved by setting the z-index to 100 at the midway point (50%) of the keyframe. Therefore, after each animation cycle, the properties are consistent with those at the beginning.

The animation is configured to repeat every 3 seconds instead of every 1 second as stated in your original code. This allows time for each iteration to run and complete before initiating the next one; hence, 3 blocks will span a duration of 3 seconds. Each block has a delay value specified in its respective CSS class.

I have excluded the unnecessary wait animation as it was found to be ineffective. If there is a need for an animation pause, alternative methods such as utilizing keyframes along with animation-delay can be explored.

@keyframes show {
  from {
  z-index: -100;
  }
  50% {
    z-index: 100;
  }
  to {
  z-index: -100;
  }
}


.block {
  position: absolute;
  z-index: -100;
  width: 100px;
  height: 100px;
  border: 1px solid black;
  animation: 3s show infinite;
}

.block-a {
    background-color: red;
    animation-delay: 0s;
}

.block-b {
    background-color: purple;
    animation-delay: 1s;
}

.block-c {
    background-color: yellow; 
    animation-delay: 2s;
}
<div class="container">
  <div class="block block-a">1</div>
  <div class="block block-b">2</div>
  <div class="block block-c">3</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

"Interactive Connect 4 Game in Javascript - Drop the disk into the bottom row of the chosen

Check out this awesome Connect4 game I found: http://codepen.io/anon/pen/lmFJf I have a specific goal in mind for the game. When I click on an empty space in any column, I want it to automatically drop into the lowest available spot in that column, follow ...

Customize your website with CSS and Bootstrap: create a transparent navbar-wrapper

Is there a way to make the menu bar background transparent without affecting the transparency of the text on the menu bar? This template is based on Twitter Bootstrap carousel. In this specific line, <div class="navbar navbar-inverse navbar-static-to ...

Steps to incorporate a personalized design onto a card element within Material UI

As a JavaScript developer, I was tasked by my brother to create a simple webpage for his business. Admittedly, frontend design is not my strongest suit so I decided to utilize Material UI and added some CSS to paste everything together. Right now, I am wo ...

Ways to access the preceding DIV element closest to the current one

Hovering over the text My will cause the image myicon.png to fade out and back in: <div class="mmItemStyleChild"> <img src="theImages/myicon.png" class="mIcon vertAlign mmm1" id="mMW1" /> <img src="theImages/emptyImg.png" class="mSpacer ...

Incorporate a CSS framework into the Angular vendor bundle

My current situation : The website is built with Angular 4 Started using Angular Starter Kit Utilizing Semantic UI as the CSS framework The challenge I'm facing : Integration of Semantic UI into webpack is not having any impact. Steps I've ...

Having trouble getting my soundboard to work properly on mobile devices

I recently created a soundboard using HTML5, CSS3, and JavaScript. While the audio plays back perfectly on my computer when I click the buttons, I encounter an issue when trying to use it on a smartphone. <!DOCTYPE html> <html lang="en"> <h ...

Organize your items vertically using jQuery Mobile's list feature

I am currently working on a mini chat application. The messages in the list are appearing side by side and I would like them to be displayed one below the other. Is there a way to adjust the CSS or add classes to achieve this? <div style="height:100% ...

Adding text to a horizontal scrolling element causes the parent div to be pushed downwards

Can anyone help me understand why inserting text into one of the divs in a horizontally scrolling div causes the parent div to move downward? Looking for suggestions! @import url('https://fonts.googleapis.com/css?family=Hind:300,400,500,600,700&apo ...

Achieve dynamic styling and adjust window size using window.open

My mind is exhausted after days of trying: function prtDiv( o ) { var css = 'body {font:normal 10px Arial;}' ; var wo = window.open('','print','width=600,height=600,resizable=yes'); wo.document.write( '<he ...

Resolving Issues with Text Overlaid on an Image Hyperlink

Alright, so this problem might be a bit too complex for CSS/HTML to handle, but I'll give it a shot anyway. I have some images that are dynamically placed using the code from this website. What I want to do with these images is add a text overlay w ...

Screen resolution for the background image on the banner

I'm currently working on a website that can be found at this temporary link: The main banner of the site features a background image with fading text, which looks great on standard desktop screens. However, when viewed on wider screens like 1600x1200 ...

Transitioning hover effects with absolutely positioned elements

I am facing an issue where I have an image with a centered absolutely positioned link on top of it. When I hover over the image, there are transition effects in place which work perfectly fine. However, when I hover over the link, these transition effects ...

Display issue with ul and li elements in Safari browser causing menu to appear incorrectly

Although I am new to CSS, I managed to create a menu on my website that hovers over the background video successfully. It is a basic menu using a ul with li elements and it appears as intended in all browsers except Safari. In Safari, the menu items stack ...

Ways to enlarge a div and its contents when hovering over it?

I am struggling to create a div that expands on hover and have been unable to make the content expand along with the div. I've experimented with various overflow options, but none of them seem to work. .grow { padding: 5px 5px 5px 5px ...

Does the html label prevent propagation from occurring?

Can anyone explain why the toggle function is not being executed when clicking inside the box with the black border, but works when clicking outside of it (although not on the checkbox)? var checks = document.querySelectorAll("ul li"); for (var i = 0 ...

Creating a mobile-friendly navigation bar with Vuetify for optimal responsiveness

I am attempting to utilize a vuetify tab component as my navigation menu in order to create a responsive navbar using vuetify. The goal is to have a normal navbar that functions like usual, but when viewed on a mobile device, it should hide the menu and di ...

Spin the div in the opposite direction after being clicked for the second time

After successfully using CSS and Javascript to rotate a div by 45 degrees upon clicking, I encountered an issue when trying to rotate it back to 0 degrees with a second click. Despite my searches for a solution, the div remains unresponsive. Any suggestion ...

Adding clickable padding to a Draft.js editor can enhance the user experience and make the editing process

Is there a way to apply padding to the Draft.js Editor so that clicking on the padding area selects the Editor? If I add padding directly to the container div of the Editor, the padding displays properly but clicking on it does not enable writing in the E ...

Glowing effects on svg shapes

I'm looking to add a pulsing light animation around an SVG half circle shape that I have created. After experimenting with CSS and Webkit, the closest I've come is achieving a pulsing light around the parent element, rather than the actual shape ...

Tips for choosing the default tab on Bootstrap

I have a question about an issue I am facing with my Angular Bootstrap UI implementation. Here is the code snippet: <div class="container" ng-controller='sCtrl'> <tabset id='tabs'> <tab heading="Title1"> ...