Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What triggers CSS transitions @keyframes?

Posted on 2014-09-16
3
Medium Priority
?
265 Views
Last Modified: 2014-09-16
Here is a code snippet. All it does is change the color of a <div> area over a certain duration.

What puzzles me is how the transition seems to trigger right away. I'm learning about CSS transitions, and I know you can trigger transitions via hover, focus, checked, etc. But this sample actually runs on its own, and I'm not sure what's triggering it. How can I trigger this transition only after a button is clicked?

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Changing color</title>
<style>
#outerWrapper {
	width: 1000px;
	height: 260px;
	margin: 30px auto;
}
@-webkit-keyframes spotLightBG {
	0% { background-color: #fff; }
	25% { background-color: #fff5a5; }
	50% { background-color: #ffd4da; }
	75% { background-color: #99d2e4; }
	100% { background-color: #d8cab4; }
}
@-moz-keyframes spotLightBG {
	0% { background-color: #fff; }
	25% { background-color: #fff5a5; }
	50% { background-color: #ffd4da; }
	75% { background-color: #99d2e4; }
	100% { background-color: #d8cab4; }
}
@-o-keyframes spotLightBG {
	0% { background-color: #fff; }
	25% { background-color: #fff5a5; }
	50% { background-color: #ffd4da; }
	75% { background-color: #99d2e4; }
	100% { background-color: #d8cab4; }
}
@keyframes spotLightBG {
	0% { background-color: #fff; }
	25% { background-color: #fff5a5; }
	50% { background-color: #ffd4da; }
	75% { background-color: #99d2e4; }
	100% { background-color: #d8cab4; }
}
div#outerWrapper {
	-webkit-animation: spotlightBG 10s infinite alternate;
	-moz-animation: spotlightBG 10s infinite alternate;
	-o-animation: spotlightBG 10s infinite alternate;
	animation: spotlightBG 10s infinite alternate;
}
</style>
</head>

<body>
<div id="outerWrapper">

</div>
</body>
</html>

Open in new window

0
Comment
Question by:elepil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40326427
Maybe have a read of this.
http://www.experts-exchange.com/A_14861-Build-a-Pure-CSS3-Slideshow-no-javascript-here.html

The important class is
div#outerWrapper {...}

Which begins the animation

To trigger it on a button click remove the ID from the div, add a class and add the ID when the button is clicked, are you using jquery?
$("button").click(function(){$(".mydiv").attr("id","outerWrapper")})

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Changing color</title>
<style>
#outerWrapper {
	width: 1000px;
	height: 260px;
	margin: 30px auto;
}
@-webkit-keyframes spotLightBG {
	0% { background-color: #fff; }
	25% { background-color: #fff5a5; }
	50% { background-color: #ffd4da; }
	75% { background-color: #99d2e4; }
	100% { background-color: #d8cab4; }
}

@keyframes spotLightBG {
	0% { background-color: #fff; }
	25% { background-color: #fff5a5; }
	50% { background-color: #ffd4da; }
	75% { background-color: #99d2e4; }
	100% { background-color: #d8cab4; }
}
div#outerWrapper {
	-webkit-animation: spotlightBG 10s infinite alternate;
	animation: spotlightBG 10s infinite alternate;
}
</style>
</head>

<body>
<div class="mydiv">

</div>
<button>Click Me</button>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$("button").click(function(){$(".mydiv").attr("id","outerWrapper")}) 
</script>
</body>
</html>

Open in new window

0
 

Author Comment

by:elepil
ID: 40326498
Gary, that link you gave me didn't really help. Let me explain where I'm coming from. I'm just starting to learn about CSS transitions, and from what I have seen so far, an event is needed to trigger the transitions (e.g. hover, active, checked, focus, etc.) which is usually initiated by the user.

But in my example, are you saying the existence of the id is all it takes to start the animation? That when using @keyframes, it's the presence or absence of the selector that causes the animation to start? Because that seems to be what you're implying with your jquery sample which creates the attribute "id='outerWrapper'" in the <div>.
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40326505
an event is needed to trigger the transitions
- Not at all, when the browser implements the CSS class on an element, as in my example above, that is when the animation begins whether dynamically or at page load as in your sample code.
The :hover etc classes are just pseudo classes that can initiate it as well.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question