2014-01-30 45 views
1

我想用javascript在我的应用程序中启动一个css动画。如何在我的情况下启动一个CSS动画?

我有这样的事情。

#ball{ 
position:absolute; 
animation-name:myfirst; 
animation-duration:5s; 
animation-timing-function:linear; 

animation-iteration-count:infinite; 
animation-direction:alternate; 
animation-play-state:running; 
/* Safari and Chrome: */ 
-webkit-animation-name:myfirst; 
-webkit-animation-duration:2s; 
-webkit-animation-timing-function:linear; 

-webkit-animation-iteration-count:1; 
-webkit-animation-direction:default; 
-webkit-animation-play-state:running; 
} 


.stage2{ 
position:absolute; 
animation-name:mySecond; 
animation-duration:5s; 
animation-timing-function:linear; 

animation-iteration-count:infinite; 
animation-direction:alternate; 
animation-play-state:running; 
/* Safari and Chrome: */ 
-webkit-animation-name:myfirst; 
-webkit-animation-duration:2s; 
-webkit-animation-timing-function:linear; 

-webkit-animation-iteration-count:1; 
-webkit-animation-direction:default; 
-webkit-animation-play-state:running; 
} 

@-webkit-keyframes myfirst /* Safari and Chrome */ 
{ 
0% {left:2%; top:47%;} 
20% {left:16%; top:47%;} 
40% {left:16%; top:58%;} 
60% {left:-10%; top:58%;} 
80% {left:-10%; top:67%;} 
100% {left:12%; top:67%;} 
} 

@-webkit-keyframes mySecond /* Safari and Chrome */ 
{ 
0% {background:red; left:2%; top:25%;} 
20% {background:yellow; left:16%; top:25%;} 
40% {background:blue; left:16%; top:35%;} 
60% {background:green; left:0; top:35%;} 
80% {background:red; left:0; top:45%;} 
100% {background:red; left:12%; top:45%;} 
} 

HTML

<img id='ball' src='test.png' /> 
<a href='#' >click </a> 

我想要当用户点击一个按钮,我开始IMG另一个动画。

JS

$('#ball').on('animationend mozanimationend webkitAnimationEnd oAnimationEnd  
    msanimationend',function(){ 
      //first animation ended // works perfect 
      $(this).hide() 
}) 

$('a').on('click', function(){ 
    $('#ball').show().addClass('stage2') 
}) 

这似乎并没有工作。任何人都可以帮助我吗?非常感谢!

+0

你把上面的代码放在你的文档就绪处理程序中吗? –

+0

@李泰勒是的,我是。谢谢你的提示:) – FlyingCat

+0

看到http://jsfiddle.net/arunpjohny/zZttg/1/ –

回答

1

如果上述脚本是您所拥有的副本,那么存在与行终止相关的语法错误。如果没有适当的字符串终止,您将在oAnimationEnd之后出现换行符。

除此之外,它看起来不错

jQuery(function() { 
    $('#ball').on('animationend mozanimationend webkitAnimationEnd oAnimationEnd msanimationend', function() { 
     //first animation ended // works perfect 
     $(this).hide() 
    }) 

    $('a').on('click', function() { 
     $('#ball').show().addClass('stage2') 
    }) 
}) 

演示:Fiddle

2

这是一个CSS问题。元素.stage2具有以下属性:

-webkit-animation-name:myfirst; 

这是错误的,因为你使用同样的动画名称都动画。

它应该是:

-webkit-animation-name:mySecond; 

除此之外,最好使用动画速记:

#ball { 
    position:absolute; 
    padding:10px; 
    -webkit-animation:myfirst 2s linear 1; 
} 
#ball.stage2 { 
    -webkit-animation:mySecond 2s linear 1; 
} 

(你还应该添加前缀和@keyframes-webkit其他浏览器)

除了这些错误,还有一个特殊的冲突,因为第一个动画会无限播放并覆盖s添加类时的第二个动画。我通过在第二个选择器#ball.stage2上添加一个id来解决这个问题 - 因此使它更具体。这将现在导致它在添加类时覆盖第一个动画。

另外,由于Arun P Johny points out,jQuery中也存在换行符。删除,并修复JS问题,它应该工作。

UPDATED EXAMPLE HERE - (-webkit唯一的 - 我还没有添加其他前缀)

你可以告诉第二个动画工作,因为背景颜色现在改变。

相关问题