Are CSS animations still triggered even when the element is not visible?

I currently have a loading indicator element that is consistently displayed on my webpage. It can be shown or hidden by adding or removing a specific class that adjusts its opacity and z-index. The element itself contains an image with a CSS animation.

My concern is that this setup may not be the most efficient for the page, as the animation could potentially continue running even when the loader is not visible.

Approaches I've Explored

I attempted to modify the animation so it only runs when the element has the visible class applied. However, this resulted in a choppy effect because the animation halted immediately upon class removal, without allowing the loader to fade out smoothly. Additionally, incorporating a transition into the animation did not yield any noticeable changes.

Answer №1

Absolutely, they do indeed. For further clarification, please refer to the following example on JS FIDDLE ==> JS FIDDLE DEMONSTRATING CHANGE

css

@keyframes anim{
    0%{margin-left:0;}
    100%{margin-left:80%;}
}
@-webkit-keyframes anim{
    0%{margin-left:0;}
    100%{margin-left:80%;}
}
h3{
    float:left;
    position:absolute;
}
div{
    visibility:hidden;
    background:black;
    width:50px;
    height:50px;
    float:left;
    animation: anim 5s;
    -webkit-animation: anim 5s infinite; /* Safari and Chrome */
}

html

<div id="div">
   Div
</div>
<h3></h3>

js

setInterval(function() {
    jQuery('h3').text('Margin is:'+ jQuery('div').css('margin-left'))
},1000);

The above code snippet illustrates the outcome within the h3 element.

Answer №2

Uncertain about whether animations continue to occur on a hidden element, one can utilize the animation-iteration-count property to conclude the animation after a specified number of iterations.

  • To conceal the loader when not in use, employ class="is-hidden".
  • Upon application of .is-hidden to the loader, modify the spinner's animation-iteration-count from infinity to a set value like 5.

This action will result in the spinner completing exactly 5 additional rotations before ceasing the animation. These rotations occur while the loader is fading out. Adjust the number of iterations accordingly if 5 spins are insufficient to cover the fade-out duration.

VIEW LIVE DEMO (toggle .is-hidden on .loader)

/* CSS */

@keyframes spin {
  to { transform: rotateZ(360deg); }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes fadeOut {
  from { opacity: 1; }  
  to   { opacity: 0; }
}

.loader {
  /* loader styles... */

  animation: fadeIn 1s forwards;
}

.spinner {
  /* spinner styles... */

  animation: spin 0.5s linear infinite;
}

.loader.is-hidden {
  animation: fadeOut 1s forwards;
}

.loader.is-hidden .spinner {
  animation-iteration-count: 5;
}

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

What steps should I take to align the name and email fields next to the text on the left and the button on the right?

Having placed an AWeber sign-up form at the top of the header on the page , I've successfully positioned the form horizontally using CSS. However, it appears misaligned. How can I correct this issue? All standard AWeber forms are in a vertical format ...

Repair the masthead background during overscroll

The Dilemma At the top of my webpage, I have a sleek masthead with a captivating background image that scrolls along with the page. However, there is an issue when users overscroll upwards, causing an undesirable white overflow to appear. To rectify this ...

Trouble with Bootstrap 3's nav-justified feature not displaying correctly upon window expansion

Looking at this Bootstrap example page, I noticed a small issue with the nav-justified navigation. When the window is minimized, it transitions correctly to a mobile version. However, when the window is maximized again, the buttons remain in the mobile for ...

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 ...

Strangely Bizarre CSS Quirk

At this website, there seems to be an issue with how the footer displays on Internet Explorer and certain older versions of Firefox. However, it appears perfectly fine on Firefox 3.6.13. Any suggestions on what could be causing this discrepancy? ...

What is the best way to insert a space between my element and pseudo-element?

I'm currently utilizing the ::before selector to insert an image before each item on my menu list. I am aiming to include a blank space between the image and the text. Below is the HTML markup <ul> <li>menu 1</li> <li>me ...

What is the best way to increase the size of this text when an image is hovered over?

I'm looking to make the .thumb_text element grow when hovering over the <img> within the li.thumb_list. Currently, I can only achieve this effect when hovering over the text itself, not the image. In addition, the text is inexplicably becoming ...

Discrepancy in Line Spacing in Internet Explorer 9 when Working with TextAreas

My TEXTAREA requires precise spacing, so I've set the formatting as shown below: TEXTAREA { font-family: Tahoma, Arial; font-size: 8pt; letter-spacing: 0px; line-height: 13px; } The issue arises when typing text into the textarea - the li ...

Javascript editing enhancement for real-time changes

Are there any tools for in-place editing of Javascript code? I'm looking for something similar to Firebug, which is great for instant CSS editing and previewing but doesn't have the capability to edit JavaScript directly. Is there a tool or addon ...

The CSS selector ">" is not functioning correctly, and neither is the "~" selector

I am currently working with the following CSS: .images_container input[type="checkbox"]:checked > label { border-color:#4378c3; opacity:1.0; } .images_container input[type="checkbox"] > label:hover { opacity:1.0; } .images_container input[type="chec ...

What is the best method to eliminate excess space between images on a webpage using CSS?

Currently, I am developing a small project just for myself. The main page will consist of multiple images, each spanning the entire page. I am having trouble eliminating this unwanted spacing: https://i.sstatic.net/7eLl5.jpg Your assistance would be gre ...

"Transforming a static navbar to a fixed position causes the page to jump

Having some difficulty figuring this out. I'm working on a bootstrap navbar that transitions from static to fixed when the user scrolls past the logo at the top. Everything seems to be working fine, except for when the navbar reaches the top, it sudde ...

Move your cursor over an image to reveal another image on top without impacting the image underneath by using background-image

Is there a way to create an effect where hovering over a specific book image will display a checkmark, indicating selection upon click? Ideally, users should be able to press alt (cmd on mac) to select multiple books simultaneously. Check out the HTML bel ...

A step-by-step guide to creating adorable rounded corners in a DIV element

I'd like to create a stylish rounded corner div on the top-right and top-left edges. I attempted it with the following code: border-top-left-radius: 5em; border-top-right-radius: 5em; Desired look of the div: https://i.stack.imgur.com/EoSvSm.jpg A ...

Utilizing Angular 2 Animations with the ngOnInit Lifecycle Hook

Suppose I envision a sleek navigation bar gracefully dropping down from the top of the browser once my app/website loads. Is it feasible to achieve this fluid motion using component animations metadata? Currently, I have managed to make it function as des ...

Using React to organize content within a material-ui layout efficiently

Currently, I am developing a React page using material-ui integrated within Electron. The main requirement is to have an AppBar containing Toolbar and Drawer components. To achieve this, I have referred to the source code from https://material-ui.com/demo ...

How can I automatically scroll to an anchor element once CSS animation has finished

I am currently working on a CSS animation that looks like this: @-webkit-keyframes flip { 0% {opacity:1;} 100% {opacity: 0;} //This could be 90% //And here could be frame for 100% which would scroll it down. } #headercover { -webkit-animation-name ...

Content in tab remains stagnant

I am having trouble creating different tabs on a page with unique content in each tab's body. Whenever I click on a new tab, the body content remains the same. I'm not sure if it's an issue with how the tabs are set up in the HTML or if ther ...

Transforming the font landscape with Twitter Bootstrap 3's Glyphicons

I have an unordered list with icons styled using Twitter Bootstrap 3. The Carme font from Google Fonts is used for normal text, and works well in that context. However, when I add icons to the unordered list using BS3, the font-family of that particular ...

How can I extract a substring from a URL and then save it to the clipboard?

Hi there! I'm working on a project for my school and could really use your expertise. I need help extracting a substring from a URL, like this one: URL = https://www.example.com/blah/blah&code=12432 The substring is: 12432 I also want to display ...