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

28

我想要以缓慢的速度动画化一个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;
}

我已经做了几个小时,但找不到任何能以亚像素级别缓慢平滑地动画化的东西。 我当前的示例是基于此页面上的示例代码制作的:http://css-tricks.com/parallax-background-css3/

我想要的动画平滑度可以在这个页面的translate()示例中看到:

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

如果不能使用background-position实现,请问是否有一种方法使用多个div来模拟重复背景,并使用translate移动这些div?


你知道这只在 Webkit 浏览器上运行,对吧? - php_nub_qq
@php_nub_qq 是的,而且翻译更兼容。例如:http://www.w3schools.com/css/css3_2dtransforms.asp - Jayden Lawson
3个回答

31

看这个例子:

#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%);
   }
}
<div id="content">Foreground content
  <div class="bg"></div>
</div>

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


这里的两个答案都不够流畅,有时会有一些延迟。我需要完全流畅的滚动... 使用3D或Z的硬件加速并不能起到任何作用,也没有其他“棘手”的CSS属性可以使其更加平滑。 是否有任何可能的方法可以实现? - Guntram
2
CSS3动画相对于脚本驱动的动画的主要优势在于性能(硬件加速)和可预测性(浏览器控制并因此优化过程)。此外,已更新结果页面以使其跨浏览器兼容:http://jsfiddle.net/5pVr4/505/。 - Oriol
@SlawaEremkin,如果您想要重复单个图像以产生无限的错觉,那么这些解决方案非常有效。然而,对于背景图案,这种技术是不可行的。我希望能够使用图案实现相同的效果。目前的CSS解决方案是通过CSS过渡动画背景位置来实现的,但效果不够流畅。 - Awin

11

使用动画background-position会导致一些性能问题。浏览器将更便宜地动画转换属性,包括translate。

这是一个使用translate进行无限滑动动画的示例(不带前缀):

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;
}

嗨,布鲁诺,我该如何编辑它以使其也能斜向移动?所以从右到左,就像现在一样,再稍微朝同一个方向倾斜? - user2513846
你可以像这样做:http://jsfiddle.net/5pVr4/723/ 我使用了一个正方形的图案并对单个元素进行动画处理,因此对角线动画在水平和垂直方向上是相同的。如果你想让它在水平方向上移动得更快,在垂直方向上移动得更慢,你可以嵌套元素并为每个元素使用不同的时间。另一种可能性是使用具有不同长宽比的图案。 - Bruno Muller

2

你需要稍微调整一下你的HTML和CSS

演示效果

HTML

<div id="wrapper">
    <div id="page">
    Foreground content
</div>

<div id="content"> </div>
</div>

CSS

@-webkit-keyframes MOVE-BG {
    from { left: 0; }
    to { left: -2000px; }
}

#wrapper {
    position:relative;
    width:800px;
    height: 300px;
    overflow:hidden;
}

#page {
    text-align: center;
    font-size: 26px;
    color: #000;
}

#content {
    width: 2000px;
    height: 300px;
    background: url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% repeat;
    position:absolute;
    top: 0;
    left: 0;
    z-index:-1;
    -webkit-animation-name: MOVE-BG;
    -webkit-animation-duration: 100s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
}

很不幸,结果看起来非常生硬。感谢您发布解决方案。 - Jayden Lawson

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