How to prevent CSS animation on page load?

余生长醉 提交于 2019-12-12 11:16:40

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/19547307/how-to-prevent-css-animation-on-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!