简体   繁体   English

添加课程,然后在添加课程后做一些事情

[英]Add a class then do something after class has been added

Hey guys i am working on a slider where i have to add a class with transform property and once the class is added fully and the animation is complete add or remove another class. 大家好,我正在使用一个滑块,在其中我必须添加一个具有transform属性的类,并且在完全添加该类并且动画完成后添加或删除另一个类。 Now this is code i did but it didn't work. 现在,这是我做的代码,但是没有用。

    $('.backgroundImage').addClass('anim', function(){
 $('.backgroundImage').removeClass('anotherClass');

Here i am trying to add a class to .backgroundImage called anim and once is fully attached remove the second class .anotherClass the idea is to do this when one class is fully attached then only other gets removed and not simultaneously. 在这里,我试图向.backgroundImage添加一个称为anim的类,并在完全连接后删除第二个类.anotherClass其想法是在一个类完全连接后执行此操作,然后仅将另一个删除而不是同时删除。 Please tell me where i am doing wrong. 请告诉我我在哪里做错了。 thanks. 谢谢。

Add/remove the next class on the transform property not on the "addClass" function. 在transform属性而不是“ addClass”函数上添加/删除下一个类。

see samples in the jquery docs. 请参阅jquery文档中的示例。 http://api.jquery.com/animate/ http://api.jquery.com/animate/

if you are using CSS3 to animate then you may want to read this post: 如果您使用CSS3进行动画处理,则可能需要阅读以下文章:

Is there a callback on completion of a CSS3 animation? CSS3动画完成时是否有回调?

Okay, if I understand your issue correctly this may help: 好的,如果我正确理解了您的问题,这可能会有所帮助:

Demo Fiddle 演示小提琴

So what I get is that you're using a CSS animation and want to do stuff after it's done. 所以我得到的是您正在使用CSS动画,并且想要在完成后做一些事情。 As far as I know the add / remove class is immediate, and there isn't an easy way to tell once a CSS animation tied to a class is complete. 据我所知,add / remove类是立即可用的,并且没有简单的方法可以告诉与该类相关联的CSS动画完成。 So you'll just have to hand code the animation time in the JS (unless someone knows a better way). 因此,您只需要在JS中手动编码动画时间即可(除非有人知道更好的方法)。

You add the first class, and then call setTimeout() on with a delay equal to your first class's animation duration. 您添加了第一堂课,然后以等于第一堂课的动画持续时间的延迟调用setTimeout() Then in the timeout you remove the old class, and add the new one. 然后在超时中删除旧的类,然后添加新的类。 In this example I set the delay to 1 second, but it could be anything. 在此示例中,我将延迟设置为1秒,但是可以是任何时间。

JS: JS:

var animationDuration = 1000; // 1 second

$('.test').addClass('yourFirstClass');

setTimeout(function() {
    $('.test').removeClass('yourFirstClass').addClass('yourSecondClass');
}, animationDuration);

The original doc (see it here) doesn't say anything about multiple parameters. 原始文档(在此处查看)未提及任何有关多个参数的内容。 Just one string per addClass call. 每个addClass调用仅一个字符串。 The examples are: 示例如下:

$( "p" ).addClass( "myClass yourClass" );

$( "p" ).removeClass( "myClass noClass" ).addClass( "yourClass" );

As of jQuery 1.4, the .addClass() method's argument can receive a function. 从jQuery 1.4开始,.addClass()方法的参数可以接收一个函数。

$( "ul li" ).addClass(function( index ) {
    return "item-" + index;
});

So I recommend you to use the following in your code: 因此,我建议您在代码中使用以下内容:

$('.backgroundImage').addClass('anim');
$('.backgroundImage').removeClass('anotherClass');

It seems to be just what you are doing (except for the function I removed). 似乎只是您在做什么(我删除的功能除外)。

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

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