Trigger CSS3 animation only once upon the page loading

I'm in the process of creating a stylish landing page using CSS3. One element that really stands out is an <a> tag with a cool animation:

@keyframes splash {
    from {
        opacity: 0;
        transform: scale(0, 0);
    }
    50% {
        opacity: 1;
        transform: scale(1.1, 1.1);
    }
    to {
        transform: scale(1, 1);
}

To take it up a notch, I decided to add a hover effect as well:

@keyframes hover {
    from {
        transform: scale(1, 1);
    }
    to {
        transform: scale(1.1, 1.1);
    }
}

However, I encountered a problem when assigning these animations:

a {
    /* Some basic styling applied here */

    animation: splash 1s normal forwards ease-in-out;
}
a:hover {
    animation: hover 1s infinite alternate ease-in-out;
}

Everything seems to work fine initially - the <a> element splashes and vibrates on hover. But once the user moves away from the element, the smooth transition ends abruptly and the animation starts over. While this behavior makes sense, it's not what I want. Is there a way to solve this issue without resorting to complex JavaScript tricks?

Answer №1

After dedicating several hours to searching online: Unfortunately, it is not achievable without the use of JavaScript. The animation-iteration-count: 1; is stored internally in the animation shorthand attribute, which is reset and overwritten on :hover. Once we remove focus from the <a> element and release the :hover, the previous class is reapplied, resulting in a reset of the animation attribute.

Regrettably, there is no method to retain a specific attribute state through different states of an element.

The only solution is to incorporate JavaScript for achieving this functionality.

Answer №2

To make sure the animation on element A plays only once, simply include this line in the styling:

animation-iteration-count: 1

This will ensure that the animation runs just one time for the specified a element.

Answer №3

To achieve this effect, you just need to add a bit of extra code.

Wrap your link in a div and then specify the animation separately.

.animateOnce {
  animation: splash 1s normal forwards ease-in-out;
}

.animateOnHover:hover {
  animation: hover 1s infinite alternate ease-in-out;
}
@keyframes splash {
    from {
        opacity: 0;
        transform: scale(0, 0);
    }
    50% {
        opacity: 1;
        transform: scale(1.1, 1.1);
    }
    to {
        transform: scale(1, 1);
    }
}

@keyframes hover {
    from {
        transform: scale(1, 1);
    }
    to {
        transform: scale(1.1, 1.1);
    }
}
<div class="animateOnce">
  <a class="animateOnHover">me!</a>
</div>

Answer №4

Just successfully tested this code snippet on both Firefox and Chrome. To customize the animation, simply add or remove the specified class below.

.animateOnce {
  -webkit-animation: YOUR-ANIMATION-NAME 0.5s normal forwards; 
  -moz-animation:    YOUR-ANIMATION-NAME 0.5s normal forwards;
  -o-animation:      YOUR-ANIMATION-NAME 0.5s normal forwards;
}

Answer №5

Simply utilize

animation: hover 1s ease-in-out forwards;

Answer №6

To easily address this issue, consider extending the duration of the animation within a:hover and utilizing transitions within @keyframes.

a:hover {
        animation: hover 200s infinite alternate ease-in-out;
    }

You can accelerate the progression of @keyframes by incorporating percentages.

@keyframes hover {
    0% {
        transform: scale(1, 1);
    }
    1% {
        transform: scale(1.1, 1.1);
    }
    100% {
        transform: scale(1.1, 1.1);
    }
}

Increasing the duration to 200 or even 300 seconds should prevent the animation from restarting unnecessarily. Realistically, most users won't spend more than a few seconds hovering over an image.

Answer №7

To achieve this effect, CSS alone is not enough and you will need to use a workaround with javascript. As some have already pointed out, the animation-iteration-count property gets reset on :hover. While it's recommended to implement everything in javascript, there may be instances where you prefer to leverage CSS for easier code customization.

Here's how you can do it in JS:

// applying a class to the html tag during the animation
const startPage = (() => {
  const html = document.documentElement,
        s = 'start';
  html.classList.add(s);
  window.addEventListener('load', function() {
    setTimeout(() => {
      html.classList.remove(s);
    }, 1500); // ensure the time matches or exceeds the duration of the CSS animation (I recommend adding a bit more).
  });
})();

As for the CSS:

/* the presence of the `.start` class triggers the animation */
.start .leaflet-marker-pane {
    animation: animDrop 1s ease;
}

Answer №8

Initially, the code provided below did not include the property "iteration-count: 1", resulting in an unexpected behavior where all line items pulsated upon entering the screen until the last item loaded, despite the absence of the 'pulse' effect being applied.

<li class="animated slideInLeft delay-1s animation-iteration-count: 1"><i class="fa fa-credit-card" aria-hidden="true"></i> 1111</li>

<li class="animated slideInRight delay-1-5s animation-iteration-count: 1"><i class="fa fa-university" aria-hidden="true"></i> 222222</li>

<li class="animated lightSpeedIn delay-2s animation-iteration-count: 1"><i class="fa fa-industry" aria-hidden="true"></i> aaaaaa</li>

<li class="animated slideInLeft delay-2-5s animation-iteration-count: 1"><i class="fa fa-key" aria-hidden="true"></i> bbbbb</li>

<li class="animated slideInRight delay-3s animation-iteration-count: 1"><i class="fa fa-thumbs-up" aria-hidden="true"></i> ccccc</li>

Answer №9

After some exploration, I stumbled upon a fix for this issue: When implementing hover animations, use the following code:

animation: hover 1s infinite alternate ease-in-out,splash 1;

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

Adjust focus dynamically on pressing enter in an HTML form

I am currently working on adjusting the functionality of my HTML input form to change focus upon pressing the enter key. I need to trigger the saveValue() function from the input field when the enter key is pressed and then move the focus to the next input ...

Styling a half circle in React Native

Despite my efforts, I have not been able to find anything quite like this: example Could anyone provide guidance on implementing this with react? ...

Customizing form designs with the combination of Django and Bootstrap

After developing a basic purchase form with custom fields, I conducted a test to ensure its functionality. The form rendered successfully, as shown below. <form id="category_form" method="post" action="/purchase/"> {% csrf_token %} { ...

Eliminate extra spacing in the panel-footer on mobile devices

Trying to eliminate the empty space displayed below the footer in mobile mode, and ensure it stretches to fill the width automatically. I have included some script-related code for reference, but omitted it as it is more logic-based and not design-related. ...

adjust the div's position based on the window's scrolling bars

Is there a way to make my div move down when the window scrolls down and up when the window scrolls up? How can I achieve this effect? ...

Position divs to be perfectly aligned and centered

I'm in the process of creating a webpage and facing challenges with aligning my divs properly, specifically the animated company logos. I want them to be positioned side by side rather than on top of each other, with space in between to fit the layou ...

Using Tailwind CSS's width property w-11/12 actually returns a full 100% width instead of the expected 91.66

It seems that the w-11/12 class is not behaving as expected. According to the documentation, it should give a width of 91.666% but instead, it's filling up 100%. This issue only occurs with the value 11, while other values work fine. Has anyone else e ...

When hovering over the JQuery drop-down menu, the menu will automatically close

While navigating the site , I noticed that when hovering over the menu everything seems to be working fine. But as soon as I attempt to access the submenu links or children, the menu unexpectedly closes. This issue may be related to having a transparent ba ...

Fast method or tool for generating a detailed CSS element list from deeply nested elements

I have been using Chrome dev tools to work on a code base that was not created by me. There are numerous deeply nested elements scattered throughout the code. I find myself having to manually search through the HTML structure in order to select them with ...

What could be causing justify-content: flex-start; to not function as expected?

Can someone help me with my flexbox navbar? I set the justify-content property to flex-start, but the content inside the container is not aligning correctly. You can see the output and code here: output html, body { backgr ...

Modify just one of the padding axis while keeping the other constant by utilizing a media query

My div currently has the following padding set: padding: 14px 150px; I want to change the left/right padding without affecting the top/bottom padding using a media query. @media screen and (max-width: 700px){ #paddingDiv{ padding: same as ...

Sizing Children in Ant Design Inline Forms

I'm having trouble making my form inline with a select taking up 60% of the space and a submit button taking up the remaining 40%. Every time I try to do this, the select and button end up sizing themselves incorrectly. The select starts out very smal ...

What is the best way to arrange form inputs in a single row?

My form consists of three input boxes. While the first two inputs are single line, the third is a description field with 10 rows set as default. However, for some reason, this third box is not aligned properly with the other two. Please refer to the screen ...

Issue with transition effect not functioning properly when hovering in HTML and CSS

Is there a way to achieve a slow smooth transition when hovering over the .bg-gradient element and have the same effect when removing the cursor? Currently, the transition happens quickly on hover. .asc { height: 500px; background-position: 50%; ...

Customize the appearance of the v-autocomplete component in Vuetify

How can I style the autocomplete component to only show words that begin with the letters I'm typing, rather than all words containing those letters? Also, is there a way to display the typed letters in bold without highlighting them? https://i.sstat ...

Permanent header that clicks into place below the primary fixed header

I've been collaborating with fellow developers on this platform to tackle a persistent issue related to a fixed header. Here is the updated fiddle for reference: http://jsfiddle.net/f95sW/ The Challenge 1) As you scroll down the page, the yellow bl ...

Optimize my webpage for a specific location

While developing a game website, I am interested in learning how to enable only specific parts of my website to function while disabling the rest. How can this be achieved? ...

What is the best way to make an element "jump to the top" when the parent element has reached its maximum height?

I have a parent div containing 6 child divs. The height of the internal divs changes dynamically based on the data. The outer div has a fixed height set. I want the internal divs to move to a new column inside the parent div when they no longer fit in term ...

One helpful tip for adjusting the size of a UI chip on the fly

I am attempting to adjust the size of a UI chip dynamically based on the font size of its parent elements using the em unit in CSS. My objective is to achieve something like this: style={{size:'1em'}} The issue I'm encountering: The chip e ...

Ways to reduce the size of the border on the bottom in HTML/CSS

Currently, I am working with a div container (utilizing bootstrap.min.css) that contains another class called divborder. The length of the border-bottom in divborder is quite long and I'm wondering how I can adjust it to be shorter. Below is a snippe ...