Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent CSS animation on page load?

I have an animation, which is taking care of the fading in and out transition on button hover state.

The problem is that the default animation (-webkit-animation: off-state 1s;) is firing off on page load. How do I make it active only after first hover state?

I know how to achieve this using CSS transitions. I am looking for a solution using animation/keyframes.

HTML

<div class="button"></div>

CSS

.button { background: #000; width: 20px; height: 20px; -webkit-animation: off-state 1s; }
.button:hover { -webkit-animation: on-state 1s; }

@-webkit-keyframes on-state {
  0% { height: 20px; }
  100% { height: 100px; }
}

@-webkit-keyframes off-state {
  0% { height: 100px; }
  100% { height: 20px; }
}

Demo

like image 235
Gajus Avatar asked Nov 12 '22 19:11

Gajus


1 Answers

As suggested by @Zeaklous, this can be done using JavaScript, e.g. using jQuery:

$('.button').one('mouseout', function () { $(this).addClass('alt-animation'); });

and moving the animation rule to .alt-animation class:

.button { background: #000; width: 20px; height: 20px; }
.button.alt-animation { -webkit-animation: off-state 1s; }
.button:hover { -webkit-animation: on-state 1s; }

Ideally, there should be CSS only alternative.

like image 83
Gajus Avatar answered Jan 04 '23 01:01

Gajus