Solved

What triggers CSS transitions @keyframes?

Posted on 2014-09-16
3
257 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 500 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

Independent Software Vendors: 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

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

752 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