Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What triggers CSS transitions @keyframes?

Posted on 2014-09-16
3
Medium Priority
?
268 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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…
Suggested Courses

886 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