简体   繁体   English

如何使用jQuery为CSS Sprite制作动画

[英]How to animate CSS sprites using jQuery

I am trying to find the simplest way to replicate a 12fps animation using CSS sprites and jQuery. 我正在尝试找到使用CSS精灵和jQuery复制12fps动画的最简单方法。

I was thinking using the setInterval() so every 83.33 millisecond, the next sprite will be loaded. 我正在考虑使用setInterval(),因此每83.33毫秒将加载下一个精灵。

My problem, is that I don't know how to do that... 我的问题是我不知道该怎么做...

I was thinking, because my sprite names are incremental like: 我在想,因为我的精灵名称是增量式的,例如:

mariohammerswing1.png 
mariohammerswing2.png 
mariohammerswing3.png
etc.

So, if we could somehow increment this until we reached the last instance, which in this case is mariohammerswing5.png it will loop back to the beginning. 因此,如果我们能以某种方式增加它,直到到达最后一个实例(在本例中为mariohammerswing5.png ,它将循环回到起点。

If I can figure out that part, I'm ready to go! 如果我能弄清楚那部分,我就准备好了! :) :)

Any suggestions? 有什么建议么?

There's a sprite-dedicated plugin for jquery 有一个专门用于jquery的精灵插件

http://www.spritely.net/ http://www.spritely.net/

Take a look ;) 看一看 ;)

Untested, but something like this: 未经测试,但是像这样:

var images = ['one.png', 'two.png', 'three.ng'];

function startAnim() {
    var $target = $('#something');
    var counter = 0;
    setTimeout(function () {
        $target.css('background-image', images[counter]);
        if (++counter > images.length - 1)
            counter = 0;
    }, 83);
}

startAnim();

You could probably apply some trickery with % (modulo) somehow, but I think it's easier to read this way. 您可能会以某种方式对% (模数)进行一些欺骗,但是我认为这种方式更容易理解。

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

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