简体   繁体   English

CSS3背景位置动画在移动设备上滞后

[英]CSS3 background-position animation lags on mobile devices

I'm trying to create a scrolling effect for a background using CSS3 animations, such as: 我正在尝试使用CSS3动画为背景创建滚动效果,例如:

body {
  background: url("bg.jpg") repeat-y 0 0;
  animation: animatedBackground 50s linear infinite;
}

@keyframes animatedBackground {
  from { background-position: 0 0; }
  to { background-position: 0 100%; }
}

( JSFiddle ) JSFiddle

This works great, except that it's very laggy on mobile devices (eg Android Chrome 43.0)). 除了在移动设备(例如Android Chrome 43.0)上非常落后之外,这非常有效。 I've tried various hacks that are suppose to force the browser to use the GPU, which sadly didn't help. 我尝试过各种旨在迫使浏览器使用GPU的黑客,可惜没有帮助。

One solution is to use translateY and duplicate the image, like shown here . 一种解决方案是使用translateY并复制图像,如此处所示 That doesn't feel very good however, since the image is pretty big to start with. 但是,这感觉不太好,因为开始时图像很大。 It does run smooth, though. 不过,它确实运行平稳。

I'm looking for alternate solutions on how to make this run smooth. 我正在寻找有关如何使其平稳运行的替代解决方案。

The reason that transform runs smoothly while background-position does not is that transform can utilize the phone's hardware acceleration while background-position must rely on the browser software's re-rendering of the element. transform不会在background-position上正常运行的原因是, transform可以利用手机的硬件加速,而background-position必须依赖浏览器软件对元素的重新渲染。 Even if it's a large image, using hardware acceleration is always better for mobile. 即使是大图,使用硬件加速总是对移动设备更好。

If it's the same image, any browser worth it's salt isn't going to incur any extra impact by using it twice, as it's cached after the first pull. 如果是同一张图片,则两次使用它都不会令人感到吃惊的浏览器都不会对其造成任何额外的影响,因为它是在第一次拉动之后进行缓存的。

So use the transform solution, and feel confident it's the right one. 因此,使用transform解决方案,并确信这是正确的解决方案。

Inspired by the link in the OP, I found a way to achieve this without having multiple references to the same image. 受到OP中链接的启发,我找到了一种无需对同一图像进行多次引用即可实现此目标的方法。 It does, however, require you to know the image's height. 但是,它确实需要您知道图像的高度。

The general idea is to have a relative wrapper which hides all overflow, and force the image to be 200% its height, and make it repeat and finally animate the y-axis -100%. 一般的想法是要有一个relative包装器,将所有溢出都隐藏起来,并迫使图像达到其高度的200%,并使其repeat并最终为y轴设置动画-100%。 Example: 例:

#parallax-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#parallax-background {
  position: relative;
  width: 100%;
  height: @parallax-image-height * 2;
  background: url("/bundles/sunnerbergsimilarseries/images/tv-show-wall.jpg") repeat 0 0;
  animation: animatedBackground 50s linear infinite;
  transform: translate3d(0, 0, 0);
}

@keyframes animatedBackground {
  0% { transform: translateY(0) }
  100% { transform: translateY(-@parallax-image-height) }
}

( JSFiddle ) JSFiddle

The above runs as smooth on a 2015 Android-phone as on a computer with a dedicated graphics card. 上面的代码在带有专用图形卡的计算机上在2015年的Android手机上运行起来一样流畅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM