【问题标题】:Animate CSS background-position with smooth results (sub-pixel animation)以平滑的结果为 CSS 背景位置设置动画(亚像素动画)
【发布时间】:2014-01-31 23:25:50
【问题描述】:

我正在尝试为 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/

如果背景位置不能做到这一点,有没有办法用多个 div 伪造重复背景并使用 translate 移动这些 div?

【问题讨论】:

标签: css animation background-image subpixel


【解决方案1】:

查看此示例:

#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 的 hw acc 没有任何作用,也没有任何其他“棘手的” css-attr 使事情变得平滑。无论如何有可能吗?
  • CSS3 动画相对于脚本驱动动画的主要优势在于性能(硬件加速)和可预测性(浏览器处于控制之中,从而优化了流程)。此外,更新了结果页面以使其跨浏览器兼容:jsfiddle.net/5pVr4/505
  • @SlawaEremkin,如果您想重复单个图像两次以产生无限的错觉,这些解决方案非常有用。但是,此技术不适用于背景图像图案。我想要同样的效果,但有一个模式。当前的 CSS 解决方案是生涩的,它通过 CSS 转换为背景位置设置动画。
【解决方案2】:

动画背景位置会导致一些性能问题。浏览器将非常便宜地为变换属性设置动画,包括翻译。

这是一个使用 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;
}

【讨论】:

  • 嗨布鲁诺,我怎样才能编辑它以在对角线上移动?所以从右到左就像现在一样加上同方向的稍微对角线?
  • 您可以这样做:jsfiddle.net/5pVr4/723 我正在使用方形图案并为单个元素设置动画,因此对角线动画在水平和垂直方向上是相同的。如果您希望它水平移动更快而垂直移动更慢,您可以嵌套元素并为每个元素使用不同的时间。其他可能性是使用具有不同纵横比的图案。
【解决方案3】:

你应该稍微调整一下你的 HTML 和 CSS

Working Demo

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

【讨论】:

  • 不幸的是,结果看起来很生涩。感谢您发布解决方案。
猜你喜欢
  • 2023-03-14
  • 2022-01-13
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多