简体   繁体   English

如何动态改变精灵的位置?

[英]how to change sprite position dynamically?

this is a sprite i found 这是我发现的精灵

在此处输入图片说明

.common-spinner.common-spinner-40x55 {
    height: 55px;
    width: 40px;
}

.common-spinner {
    background: url("images/loading-sprite.png") no-repeat scroll 100% 100% transparent;
}

<div class="loading">
<div class="common-spinner common-spinner-40x55" style="background-position: 0px 0px;"></div>
</div>

any idea how to build the loading animation from this? 任何想法如何从中构建加载动画? i tried to change position using a for loop like 我试图使用for循环来更改位置

for(i=0; i<=720;) {
    $('.common-spinner').css('background-position', '-'+i+'px 0px');
    i=i+20;
}

but i can not see any animation maybe because its going too fast? 但我看不到任何动画,可能是因为动画进行得太快了?

any idea how to do this? 任何想法如何做到这一点?

Regards 问候

Ive added the code to jsfiddle with Erik Hesselink solution Ive使用Erik Hesselink解决方案将代码添加到jsfiddle中

http://jsfiddle.net/X7tGb/ http://jsfiddle.net/X7tGb/

To actually see the animation, you have to leave the Javascript execution thread. 要实际观看动画,您必须离开Javascript执行线程。 This can be done by using timeouts. 这可以通过使用超时来完成。 Something like: 就像是:

function setBgPosition (px)
{
  return function ()
  {
    $('.common-spinner').css('background-position', '-' + px + 'px 0px');
    if (px < 720) setTimeout(setBgPosition(px + 20), 100);
  }
}

setTimeout(setBgPosition(0), 100);

http://addyosmani.com/blog/jquery-sprite-animation/ http://addyosmani.com/blog/jquery-sprite-animation/

You may want to try Google there are a ton of results regarding JavaScript sprite animation. 您可能想尝试Google,关于JavaScript Sprite动画有很多结果。 The above is one of those results with a working demo. 以上是使用演示程序的结果之一。

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

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