jon king jon king - 29 days ago 17
CSS Question

Make CSS/HTML image shake code trigger without hover/mouseover

Here's my problem, I have my code here, and it works. Only thing is, I can't seem to make it work when I take away

.shake:hover,
You see, I want the image to shake continuously, regardless if it's being hover over or not. In short, I want it to run by itself.

Here's the fiddle

CSS

body{
background-color:transparent;
}
img{
display: block;
height: 250px;
width: 250px;
}
@-webkit-keyframes spaceboots {
0% { -webkit-transform: translate(2px, 1px) rotate(0deg); }
10% { -webkit-transform: translate(-1px, -2px) rotate(-1deg); }
20% { -webkit-transform: translate(-3px, 0px) rotate(1deg); }
30% { -webkit-transform: translate(0px, 2px) rotate(0deg); }
40% { -webkit-transform: translate(1px, -1px) rotate(1deg); }
50% { -webkit-transform: translate(-1px, 2px) rotate(-1deg); }
60% { -webkit-transform: translate(-3px, 1px) rotate(0deg); }
70% { -webkit-transform: translate(2px, 1px) rotate(-1deg); }
80% { -webkit-transform: translate(-1px, -1px) rotate(1deg); }
90% { -webkit-transform: translate(2px, 2px) rotate(0deg); }
100% { -webkit-transform: translate(1px, -2px) rotate(-1deg); }
}
.shake:hover,
.shake:focus {
-webkit-animation-name: spaceboots;
-webkit-animation-duration: 0.8s;
-webkit-transform-origin:50% 50%;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}


HTML

<body>
<center>
<section class="content">
<h1></h1>
<br>
<br>
<img src="http://image.flaticon.com/teams/new/1-freepik.jpg" class="shake">
<h2 class="shake"></h2>
</section>
</center>
</body>

Answer

You need simply to remove :hover and .shake:focus, so it would look like that:

.shake {
  -webkit-animation-name: spaceboots;
  -webkit-animation-duration: 0.8s;
  -webkit-transform-origin: 50% 50%;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-timing-function: linear;
}

Working fiddle: http://jsfiddle.net/fthLwct0/2