动画 CSS 背景位置具有平滑的结果(子像素动画) [英] Animate CSS background-position with smooth results (sub-pixel animation)

查看:28
本文介绍了动画 CSS 背景位置具有平滑的结果(子像素动画)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试缓慢地为 div 的背景位置设置动画,但没有生涩的运动.你可以在这里看到我目前努力的结果:

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

@-webkit-keyframes MOVE-BG {从 {背景位置:0% 0%}到 {背景位置:187% 0%}}#内容 {宽度:100%;高度:300px;背景:url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% 重复;文本对齐:居中;字体大小:26px;颜色:#000;-webkit-animation-name: MOVE-BG;-webkit-animation-duration: 100s;-webkit-animation-timing-function:线性;-webkit-animation-iteration-count:无限;}

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

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

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

如果不能通过背景位置完成,有没有办法用多个 div 伪造重复背景并使用翻译移动这些 div?

解决方案

查看此示例:

#content {高度:300px;文本对齐:居中;字体大小:26px;颜色:#000;位置:相对;}.bg{位置:绝对;左:0;右:0;顶部:0;底部:0;z-索引:-1;背景:url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% 重复;动画名称:MOVE-BG;动画持续时间:100s;动画计时功能:线性;动画迭代次数:无限;}@keyframes MOVE-BG {从 {变换:translateX(0);}到 {变换:translateX(-187%);}}

前景内容<div class="bg"></div>

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

I'm trying to animate the background-position of a div, slowly, but without it having jerky movement. You can see the result of my current efforts here:

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

I have been at this for hours and can't find anything that will animate slowly and smoothly at a sub-pixel level. My current example was made from the example code on this page: http://css-tricks.com/parallax-background-css3/

The smoothness of animation I'm after can be seen on this page's translate() example:

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

If it can't be done with the background-position, is there a way to fake the repeating background with multiple divs and move those divs using translate?

解决方案

Checkout this example:

#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/

这篇关于动画 CSS 背景位置具有平滑的结果(子像素动画)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆