当鼠标悬停时更改动画速度

9
我正在构建一个行星系统,可以在轨道上悬停并且动画速度会改变。 我尝试了使用onMouseOver和.hover jquery,但我无法弄清楚为什么它不起作用。

#universum-planet1 {
  position: absolute;
  height: 250px;
  width: 250px;
  top: 40%;
  left: 50%;
  margin-left: -125px;
  margin-top: -65px;
  z-index: 1;
  border: 1px solid #989898;
  -webkit-border-radius: 225px;
  -moz-border-radius: 225px;
  border-radius: 225px;
  -webkit-animation: spin 15s linear infinite;
  -moz-animation: spin 15s linear infinite;
  animation: spin 15s linear infinite;
  -moz-transform-origin: center center;
  -webkit-transform-origin: center center;
  transform-origin: center center;
}
#planet1 {
  position: absolute;
  top: 5%;
  left: 5%;
  height: 50px;
  width: 50px;
  z-index: 2;
  -webkit-animation: spin 30s linear infinite;
  -moz-animation: spin 30s linear infinite;
  animation: spin 30s linear infinite;
  -moz-transform-origin: center center;
  -webkit-transform-origin: center center;
  transform-origin: center center;
}
@-moz-keyframes spin {
  100% {
    -moz-transform: rotate(360deg);
  }
}
@-webkit-keyframes spin {
  100% {
    -webkit-transform: rotate(360deg);
  }
}
@keyframes spin {
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}
<div id="universum-planet1">
  <div id="planet1">
    <a href="universe.html" id="enterLink">
      <img class="enter" src="http://webmaths.files.wordpress.com/2009/03/soccerball1.png" style="height:100%;" alt="" onMouseOver="this.src='http://www.rsg-shop.com/images/Ball-PASTORELLI-Giallo-Fluo-00014-0.jpg';" onMouseOut="this.src='http://webmaths.files.wordpress.com/2009/03/soccerball1.png'"
      />
    </a>
  </div>
</div>


1
有趣,我预期悬停更改会起作用。 - Shomz
1个回答

6
这个解决方案使用了一种包装器来包裹轨道,使其具有相同的关键帧动画(反向)并且可以在悬停时暂停/运行以改变动画速度:
演示:DEMO

.wrapper{
  position:absolute;
  top:40%; left:50%;
  margin-left:-125px;
  margin-top:-65px;
  width: 250px; height:250px;

  -webkit-animation:spin 20s linear infinite;
  -moz-animation:spin 20s linear infinite;
  animation:spin 20s linear infinite;

  -webkit-animation-direction: reverse;
  -moz-animation-direction: reverse;
  animation-direction: reverse;

  -webkit-animation-play-state: paused;
  -moz-animation-play-state: paused;
  animation-play-state: paused;
}

#universum-planet1 {
  height:250px; width: 250px;
  z-index:1;
  border: 1px solid #989898;
  border-radius: 225px;
  -webkit-animation:spin 15s linear infinite;
  -moz-animation:spin 15s linear infinite;
  animation:spin 15s linear infinite;
  -moz-transform-origin:center center;
  -webkit-transform-origin:center center;
  transform-origin:center center;
}
#planet1 {
  position:absolute;
  top:5%; left:5%;
  height: 50px; width: 50px;
  z-index:2;
  -webkit-animation:spin 30s linear infinite;
  -moz-animation:spin 30s linear infinite;
  animation:spin 30s linear infinite;
  -moz-transform-origin:center center;
  -webkit-transform-origin:center center;
  transform-origin:center center;
}
.wrapper:hover{
  -webkit-animation-play-state: running;
  -moz-animation-play-state: running;
  animation-play-state: running;
}

@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
<div class="wrapper">
  <div id="universum-planet1">
    <div id="planet1"> <a href="universe.html" id="enterLink">
      <img class="enter" src="http://webmaths.files.wordpress.com/2009/03/soccerball1.png" style="height:100%;" alt="" onMouseOver=   "this.src='http://www.rsg-shop.com/images/Ball-PASTORELLI-Giallo-Fluo-00014-0.jpg';" onMouseOut="this.src='http://webmaths.files.wordpress.com/2009/03/soccerball1.png'" /></a> 
    </div>
  </div>
</div>

这是受到Vals这个回答的启发而来的。


不错。为什么我在这个fiddle中尝试的内容不起作用?但是这个fiddle却可以! - MilkyTech
@ChrisM 在你的fiddle中,这个类正在切换,但是你不能像那样覆盖动画的速度。 - web-tiki
我不明白为什么。我甚至尝试了addClass和removeClass。如果JavaScript去除当前类,原始动画速度不应该只是消失,然后带入一个新的具有不同速度的类吗? - MilkyTech
@ChrisM 看起来.removeClass可以正常工作,请参考这里:http://jsfiddle.net/webtiki/XE5xm/41/ - web-tiki
@ChrisM 是的,我刚试了一下,你是对的...不知道为什么,我得再深入挖掘一下。 - web-tiki
@ChrisM 这段代码虽然可以工作,但它会在鼠标悬停时从开头开始动画。http://jsfiddle.net/webtiki/2D3BC/2/ - web-tiki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接