0

我想在每次点击 change_photo 按钮时移动 div 但这段代码只运行一次,我不需要无限循环,我希望每次点击时 div 移动。

这是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>

这是html代码:

<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>
4

2 回答 2

1

使用 css3 过渡 -

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

删除 Javascript 和关键帧规则,这将起作用
编辑:
jsfiddle using jquery.animate()

于 2013-01-18T14:43:20.687 回答
0
@-webkit-keyframes myfirst
{
    0%   {left:0}
   50%   {left:100%}
  100%   {left:0}
}
于 2013-01-18T15:08:24.587 回答