没有补间的 CSS3 Sprite 动画

发布于 2024-10-08 13:18:13 字数 1899 浏览 0 评论 0原文

阅读下面的内容以了解我的最终编辑!

是否可以使用 CSS3 动画而不在帧之间进行补间动画?

例如,我有一个图像,上面有两个角色动画精灵。它们均匀间隔 50px。当我使用以下动画时,我仍然得到补间(尽管补间非常快,因此看起来像闪烁)。

#ball .animated{
        -webkit-animation-name: animate;
        -webkit-animation-duration: .5s;
        -webkit-animation-iteration-count: infinite;
        -webkit-animation-direction: alternate;
        -webkit-animation-timing-function: linear;}
@-webkit-keyframes animate{
        0%{-webkit-transform: translate3d(0,0,0);}
        49%{-webkit-transform: translate3d(0,0,0);}

        50%{-webkit-transform: translate3d(-50px,0,0);}
        100%{-webkit-transform: translate3d(-50px,0,0);}

因此,根据上述内容,精灵帧应在前 0-49% 的持续时间内保持在图像的第一部分 (x = 0px),然后跳转到图像的第二部分 (x = -50px) 50-100%。然而,1% 的差异仍然足以在视觉上看到 0 到 -50px 的补间。

想法?

编辑:

-webkit-animation-timing-function: cubic-bezier(1,0,1,0);

上面的内容似乎稍微理顺了它,但过了一会儿它又回到闪烁状态。

编辑: 我没有意识到你可以使用小数和百分比。将差距从 1% 缩小到 0.1% 会创建更快的补间,但几乎不可见(使用 -webkit-animation-duration: < 1s;)

0%{-webkit-transform: translate3d(0,0,0);}
49.9%{-webkit-transform: translate3d(0,0,0);}

50%{-webkit-transform: translate3d(-50px,0,0);}
100%{-webkit-transform: translate3d(-50px,0,0);}

最终编辑!:< /强> 好的,根据我发现的网络工具包动画百分比将接受小数到百万位(即 0.0001)。在相对较快的动画计时器上,这将导致瞬时翻译。我想有点黑客行为,但它确实有效。

示例:

@-webkit-keyframes sprite {
 0% {
   -webkit-transform: translate3d(0,0,0);
 }
 50% {
   -webkit-transform: translate3d(0,0,0);
 }
 50.0001%{
   -webkit-transform: translate3d(-50px,0,0);
 }
 100%{
   -webkit-transform: translate3d(-50px,0,0);
 }
}

上面的示例是容器 div 内的 100 像素图像(图像上的每个精灵宽 50 像素),且 width: 50pxoverflow:hidden 仅一次显示图像上的一个精灵。

注意:我使用translate3d,因为它在移动浏览器中进行硬件加速,其中translateX、translateY、translateZ 尚未硬件加速。

Read below for my final edit!

Is it possible to use CSS3 animations without having the animations tween between frames?

For example, I have an image that I have two character animation sprites on. They are spaced evenly 50px. When I use the following animation I still get a tween (although a very fast tween so it can look like a flicker).

#ball .animated{
        -webkit-animation-name: animate;
        -webkit-animation-duration: .5s;
        -webkit-animation-iteration-count: infinite;
        -webkit-animation-direction: alternate;
        -webkit-animation-timing-function: linear;}
@-webkit-keyframes animate{
        0%{-webkit-transform: translate3d(0,0,0);}
        49%{-webkit-transform: translate3d(0,0,0);}

        50%{-webkit-transform: translate3d(-50px,0,0);}
        100%{-webkit-transform: translate3d(-50px,0,0);}

So based on the above, the sprite-frame should be held on the first part of the image (x = 0px) for the first 0-49% of the duration and then jump to second part of the image (x = -50px) for 50-100%. However, the 1% difference is still enough to visually see a tween from 0 to -50px.

Thoughts?

Edit:

-webkit-animation-timing-function: cubic-bezier(1,0,1,0);

The above seemed to straighten it out a bit but after a while it goes back to flickering.

Edit:
I hadn't realized that you could use decimals with the percentages. Closing the gap from 1% to 0.1% creates a much faster tween which is just about not visible (with a -webkit-animation-duration: < 1s;)

0%{-webkit-transform: translate3d(0,0,0);}
49.9%{-webkit-transform: translate3d(0,0,0);}

50%{-webkit-transform: translate3d(-50px,0,0);}
100%{-webkit-transform: translate3d(-50px,0,0);}

Final edit!:
Ok, so from what I've found web-kit animations percentages will accept a decimal to the millionth place (i.e. 0.0001). Which on a relatively quick animation timer will result in an instantaneous translation. A little bit of a hack I suppose but it does the trick.

Example:

@-webkit-keyframes sprite {
 0% {
   -webkit-transform: translate3d(0,0,0);
 }
 50% {
   -webkit-transform: translate3d(0,0,0);
 }
 50.0001%{
   -webkit-transform: translate3d(-50px,0,0);
 }
 100%{
   -webkit-transform: translate3d(-50px,0,0);
 }
}

The above example is of an image of 100px (each sprite on the image is 50px wide) within a container div with the width: 50px and overflow:hidden to only show one sprite off the image at a time.

Note: I am using translate3d because it is hardware accelerated in mobile browsers where translateX,translateY,translateZ are not yet hardware accelerated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二货你真萌 2024-10-15 13:18:13

这是另一个使用 步骤()

这是一种简单而强大的精灵动画方式。下面是老公爵挥手的动画。

@keyframes wink {
    from { background-position: 0px; }
    to { background-position: -500px; }
}

.hi {
    width: 50px;
    height: 72px;
    background-image: url("https://i.sstatic.net/1Ad8o.png");
    margin: 0 auto;      
    animation: wink .8s steps(10, end) infinite;
}
<img src="https://i.sstatic.net/1Ad8o.png">
<div class="hi"></div>

您可以在 cssdeck 上玩一个演示。

Here is another great example using steps().

It is a simple yet powerful way for animating sprites. Below there's an animation of old Duke waving.

@keyframes wink {
    from { background-position: 0px; }
    to { background-position: -500px; }
}

.hi {
    width: 50px;
    height: 72px;
    background-image: url("https://i.sstatic.net/1Ad8o.png");
    margin: 0 auto;      
    animation: wink .8s steps(10, end) infinite;
}
<img src="https://i.sstatic.net/1Ad8o.png">
<div class="hi"></div>

There's a demo you can play with on cssdeck.

格子衫的從容 2024-10-15 13:18:13

自从提出这个问题以来已经有一段时间了,但是 CSS3 现在具有步进计时功能,因此我将其用于精灵动画。来自我的 codepen 示例 http://codepen.io/natedsaint/pen/2/7

/* Animation keyframes */
@keyframes walk {
  0% { background-position:0px 0px;}
  16.67% { background-position:-104px 0px;}
  33.33% { background-position:-208px 0px;}
  50% {background-position:-320px 0px;}
  66.66% { background-position:-416px 0px;}
  80.65% { background-position:-520px 0px;}
  100% { background-position:-624px 0px;}
}

#guyBrush {
  animation: walk infinite 1s steps(1,end);
  background-image:url('http://www.nathanstpierre.com/gb_walk.png');
  width:104px;
  height:152px;
  position:absolute;
  top:160px;
  left:360px;
} 

这样做的好处是您可以通过将动画的持续时间更改为较小的数字来改变速度。我已经实现了一个滑块来显示这一点。

It's been awhile since this question was asked, but CSS3 now has a step timing function, so I've used that for sprite animations. From my codepen example at http://codepen.io/natedsaint/pen/2/7 :

/* Animation keyframes */
@keyframes walk {
  0% { background-position:0px 0px;}
  16.67% { background-position:-104px 0px;}
  33.33% { background-position:-208px 0px;}
  50% {background-position:-320px 0px;}
  66.66% { background-position:-416px 0px;}
  80.65% { background-position:-520px 0px;}
  100% { background-position:-624px 0px;}
}

#guyBrush {
  animation: walk infinite 1s steps(1,end);
  background-image:url('http://www.nathanstpierre.com/gb_walk.png');
  width:104px;
  height:152px;
  position:absolute;
  top:160px;
  left:360px;
} 

The benefit to this is that you can alter the speed by changing the duration of the animation to a lower number. I've implemented a slider to show this.

若水微香 2024-10-15 13:18:13

CSS 动画的总体思想是制作动画。如果您希望事物从一个位置跳转到另一个位置,那么您可能只需考虑直接通过 JavaScript 设置位置并使用 JavaScript 进行迭代。

但是,如果您确实想使用动画,您有几个选择。一种是将不透明度设置为零,然后使用两个填充关键帧将其恢复为一。或者更改 z-index 以在翻译发生时将动画对象隐藏在遮罩 div 后面。 z 索引不补间。

更新:步骤函数转换已添加到规范中,并且现已在 Chrome 中实现,因此现在您想要做的事情是可能的。

The general idea of CSS Animation is to, well, animate. If you want things to jump from position to position, then you might just consider setting position directly via JavaScript and doing your iterations with JavaScript.

However if you do want to use animations, you have a few options. One is setting the opacity to zero and back to one with two filler keyframes. Or alternatively changing z-index to hide your animating object behind a masking div while the translation happens. z-indexes don't tween.

UPDATE: Step function transitions have been added to the spec and are now implemented in Chrome, so now what you wanted to do is possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文