简体   繁体   English

使用Javascript / jQuery使用Spritesheet从一类过渡到另一类

[英]Fading from one class to another with a spritesheet using Javascript/jQuery

I have a spritesheet with plenty of images which I switch to depending on the character's feelings. 我有一个带有大量图像的Spritesheet,我会根据角色的感觉切换到这些图像。 I've mapped out all of his expressions into classes. 我已经将他所有的表达式都映射到了类中。

.normal{
    background: url(background.png) 0 0;
}

.happy{
    background: url(background.png) 100% 0;
}

Depending on the circumstances, I remove the class from the div then give it the new class, such as this: 根据具体情况,我从div中删除该类,然后为其提供新的类,例如:

$('#div1').removeClass();
$('#div1').addClass(happy);

I'd like to make the transition between these smoother by fading from one image to the other. 我想通过从一个图像过渡到另一个图像来使这些过渡更加平滑。 I've searched around and most examples I've found mentioned either a :hover css, but I want to be able to control it via Javascript code and not on a mouseover. 我到处搜索,发现的大多数示例都提到了:hover css,但是我希望能够通过Javascript代码而不是鼠标悬停来控制它。 There was also one solution I found that was to use transition: background 0.5s ease-in-out; 我发现还有一个解决方案是使用transition: background 0.5s ease-in-out; , but this solution only moves the spritesheet from the first section to the other. ,但此解决方案仅将Spritesheet从第一部分移到另一部分。 Others suggested $('#div1').removeClass(currExpression).addClass(expression).fadeIn(500); 其他人建议$('#div1').removeClass(currExpression).addClass(expression).fadeIn(500); , but it doesn't make a difference. ,但没有影响。

I also decided to do the transitioning with a good old 我也决定用一个好老

$('#div1').fadeOut();
$('#div1').removeClass();
$('#div1').addClass(expression);
$('#div1').fadeIn();

But it doesn't quite do what I want, as it fades away and fades in instead of fading from one class to another directly. 但这并不能完全满足我的要求,因为它会逐渐消失并逐渐消失,而不是直接从一类过渡到另一类。

What I'd really like is to do as if the first section of my sprite sheet would fade into the second section of the sprite sheet as if they were two separate images, a bit like this: http://tinypic.com/view.php?pic=jv6wet&s=9#.VwCUO6QrK5g . 我真正想做的是,将精灵表的第一部分淡入精灵表的第二部分,就像它们是两个单独的图像一样,有点像这样: http : //tinypic.com/view .php?pic = jv6wet&s = 9#.VwCUO6QrK5g Is there a possible way to do this in jQuery, CSS or Javascript? 有没有可能在jQuery,CSS或Javascript中执行此操作?

this can be done with css... you forgot to add the transition timing function property and that might be what is going on. 可以使用CSS完成此操作...您忘记添加过渡计时功能属性,可能就是这种情况。 Try 尝试

transition: background 0.5s ease;

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

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