Solved

What triggers CSS transitions @keyframes?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now