简体   繁体   English

jQuery:如何等到fadeTo(或任何其他效果)完成之后再执行代码

[英]jQuery: How to wait until fadeTo(or any other effect) finishes before executing code

I'm making a news board that fades the news in and out. 我正在制作一个新闻板,淡入淡出新闻。 When the element fades out the content gets deleted, when it fades back in there is new content in its place. 当元素淡出时,内容将被删除,而当元素淡出时,其位置将包含新内容。 This works, just the timing is off. 这是可行的,只是时间到了。 The new content that should only appear when the old content has faded away, appears as the element is fading. 仅当旧内容淡出后才应显示的新内容会随着元素的褪色而出现。 Is there a way to wait until this is done? 有没有办法等到完成?

$(document).ready(function() {
    var counter = 0;
    var slides = ['<p>Track All of Your enemys Using The <span>Scout Tool!</span></p>',
                    '<p>Download powerups Using The <span>Pirate Tool!</span></p>',
                    '<p>Gather Badges To Attack others in <span>Raids!</span></p>',
                    '<p>Enjoy Orgainzing Your Profile <span>Join Now!</span></p>'];

    $('#slide-left').click(function() {
        $('#slide').fadeTo('fast', 0);
        $('#slide p').remove();

        if (counter > 0){counter --;}

        $('#slide').append(slides[counter]);
        $('#slide').fadeTo('fast', 1);
    });
    $('#slide-right').click(function() {
        $('#slide').fadeTo('fast', 0);
        $('#slide p').remove();

        if (counter < (slides.length-1)){counter ++;}

        $('#slide').append(slides[counter]);
        $('#slide').fadeTo('fast', 1);

    });

});

fadeTo accepts a third argument which is the callback function . fadeTo接受第三个参数,即回调函数 This is called when the animation completes. 动画完成时将调用此方法。 So you could always do something like : 因此,您始终可以执行以下操作:

$('#slide').fadeTo('fast', 1, function() {

    // This part is executed after fade

});

you can do 你可以做

$('#slide').fadeTo('fast', 0,function(){
     $('#slide p').remove();
     if (counter > 0){counter --;}
     $('#slide').append(slides[counter]).fadeTo('fast', 1);
});
$(document).ready(function() {
    var counter = 0;
    var slides = ['<p>Track All of Your enemys Using The <span>Scout Tool!</span></p>',
                    '<p>Download powerups Using The <span>Pirate Tool!</span></p>',
                    '<p>Gather Badges To Attack others in <span>Raids!</span></p>',
                    '<p>Enjoy Orgainzing Your Profile <span>Join Now!</span></p>'];

    $('#slide-left').click(function() {
        $('#slide').fadeTo('fast', function() {
            $('#slide p').remove();
            if (counter > 0){
                counter --;
            }else{
                if (counter < (slides.length-1)){
                    counter ++;
                }
            }
            $('#slide').append(slides[counter]);
            $('#slide').fadeTo('fast', 1);
        });
    });
});

I don't know it this is correct or not, but my 5 sents : 我不知道这是正确还是错误,但我的5条消息:

Hope i have helped, 希望我有所帮助,

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

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