使用平滑结果动画CSS背景位置(子像素动画)

Jay*_*son 24 animation background-image css3 subpixel

我正试图慢慢地为div的背景位置设置动画,但没有它有生涩的动作.您可以在此处查看我当前工作的结果:

http://jsfiddle.net/5pVr4/2/

@-webkit-keyframes MOVE-BG {
    from {
        background-position: 0% 0%
    }
    to { 
        background-position: 187% 0%
    }
}

#content {
    width: 100%;
    height: 300px;
    background: url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% repeat;
    text-align: center;
    font-size: 26px;
    color: #000;

    -webkit-animation-name: MOVE-BG;
    -webkit-animation-duration: 100s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
}
Run Code Online (Sandbox Code Playgroud)

我已经在这里工作了几个小时,找不到任何可以在亚像素级别缓慢而平滑地制作动画的东西.我当前的示例来自此页面上的示例代码:http://css-tricks.com/parallax-background-css3/

我可以在这个页面的translate()示例中看到我所追求的动画的流畅度:

http://css-tricks.com/tale-of-animation-performance/

如果无法使用background-position完成,是否有办法用多个div伪造重复背景并使用translate移动这些div?

Sla*_*kin 25

查看此示例:

http://jsfiddle.net/5pVr4/4/

#content {
  height: 300px;
  text-align: center;
  font-size: 26px;
  color: #000;
  position:relative;
}
.bg{
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: -1;
  background: url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% repeat;
  animation-name: MOVE-BG;
  animation-duration: 100s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}
@keyframes MOVE-BG {
   from {
     transform: translateX(0);
   }
   to { 
     transform: translateX(-187%);
   }
}
Run Code Online (Sandbox Code Playgroud)

  • CSS3 动画相对于脚本驱动动画的主要优势是性能(硬件加速)和可预测性(浏览器处于控制之中,从而优化流程)。另外,更新了结果页面以使其跨浏览器兼容:http://jsfiddle.net/5pVr4/505/ (2认同)

小智 6

动画背景位置会导致一些性能问题。浏览器将以低廉的价格为转换属性(包括翻译)制作动画。

这是一个为无限幻灯片动画(不带前缀)使用平移的示例:

http://jsfiddle.net/brunomuller/5pVr4/504/

@-webkit-keyframes bg-slide {
    from { transform: translateX(0); }
    to { transform: translateX(-50%); }
}

.wrapper {
    position:relative;
    width:400px;
    height: 300px;
    overflow:hidden;
}

.content {
    position: relative;
    text-align: center;
    font-size: 26px;
    color: #000;
}

.bg {
    width: 200%;
    background: url(http://www.gstatic.com/webp/gallery/1.jpg) repeat-x;
    position:absolute;
    top: 0;
    bottom: 0;
    left: 0;
    animation: bg-slide 20s linear infinite;
}
Run Code Online (Sandbox Code Playgroud)