user1230622
user1230622

Reputation: 1

I want to animate image every click in css3

I want to move div when every click to change_photo button but this code run only one time ,I don't need infinite loop ,i want div moves when every click.

this is css:

<style>
      @-webkit-keyframes myfirst
{
0%   {left:0px}
100% {left:100%}
}
#images {
  width: 1000px;
  height: 300px;
  overflow: hidden;
  position: relative;
  margin: 20px auto;
}
#im{ position:relative;}
    </style>

this is html code:

<div id="images" style="">
  <img id="im" src="images/banner.jpg" 
            style="width: 833px; height: 179px;" />
            </div>
            <input type="button" value="change_photo" onclick="animate();" />
    </div>

<script>
function animate() {
        var im = document.getElementById("im");
        im.style.webkitAnimation = "myfirst 3s";
    }
</script>

Upvotes: 0

Views: 101

Answers (2)

svineet
svineet

Reputation: 1889

Use css3 transitions-

#image{
-webkit-transition:all 2s;
}
#image:hover{
left:100px;
} 

Remove Javascript and the keyframes rule and this will work
EDIT:
jsfiddle using jquery.animate()

Upvotes: 1

Mesh
Mesh

Reputation: 6422

@-webkit-keyframes myfirst
{
    0%   {left:0}
   50%   {left:100%}
  100%   {left:0}
}

Upvotes: 0

Related Questions