html - CSS3 动画位置

标签 html animation css

考虑 CSS3 动画,船在蓝色 div 上方移动。由于某种原因,船没有移动。 HTML 如下:

<div id="wrapper">
  <div id="sea">
    <img src="ship.png" alt="ship" width="128" height="128"/>
  </div>
</div>

为了制作 CSS3 动画,我使用了以下内容:

#wrapper { position:relative;top:50px;width:700px;height:320px;
          margin:0 auto;background:white;border-radius:10px;}
#sea { position:relative;background:#2875DE;width:700px;height:170px;
       border-radius:10px;top:190px; }
#sea img { 
  position:relative;left:480px;top:-20px;
  animation:myship 10s;
  -moz-animation:myship 10s; /* Firefox */
  -webkit-animation:myship 10s; /* Safari and Chrome */
  @keyframes myship {
    from {left: 480px;} 
    to{left:20px;} 
   }
   @-moz-keyframes myship {
     from {left: 480px;} 
     to {left:20px;} 
   }
   @-webkit-keyframes myship {
     from {left: 480px;} 
     to{left:20px;} 
   }
}

飞船图像没有移动。非常感谢任何帮助。

最佳答案

您必须在 css 选择器之外声明您的关键帧,并为绝对定位的元素设置动画。

http://jsfiddle.net/aNvSf/

你修改后的 css 看起来像这样:

#wrapper{
    position:relative;
    top:50px;
    width:700px;
    height:320px;
    margin:0 auto;
    background:white;
    border-radius:10px;
}
#sea{
    position:relative;
    background:#2875DE;
    width:700px;
    height:170px;
    border-radius:10px;
    top:190px;
}
#sea img{
    position:absolute;
    left:480px;
    top:-20px;
    animation:myship 10s;
    -moz-animation:myship 10s; /* Firefox */
    -webkit-animation:myship 10s; /* Safari and Chrome */               
}

@keyframes myship{
    from {left: 480px;} 
    to{left:20px;} 
}
@-moz-keyframes myship{
    from {left: 480px;} 
    to{left:20px;} 
}
@-webkit-keyframes myship{
    from {left: 480px;} 
    to{left:20px;} 
}​

关于html - CSS3 动画位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10424500/

相关文章:

animation - 如何在投影仪演示中更改图像时修复 block 的位置?

html - 当我以 % 为单位分配边距时,它引用的是宽度而不是宽度和高度

html - li 中的文本未水平对齐

php - 为 html 目录中的每个文件创建一个按钮

php - 在 php 中用表而不是行填充我的下拉列表?

javascript - 框架7 : Navbar hidden when I'm move to anothers views

html - 为什么在 Chrome 和 Firefox 中元素宽度不同?

css - 简单的 CSS 布局 - 我怎样才能实现这种布局,并使其易于扩展?

动画结束后的 CSS 过渡

javascript - 我如何将 CSS 动画链接到 javascript 日期对象