Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

What triggers CSS transitions @keyframes?

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

Avatar of Gary
Gary
Flag of Ireland image

Maybe have a read of this.
https://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

Avatar of elepil
elepil

ASKER

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>.
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial