简体   繁体   English

无法获取元素动画以在jQuery中等待另一个

[英]Unable to get an element animation to wait for another in jquery

If you go to an album (eg. Nature because still working on the others) and click one of the images they all fade out and then the one you clicked appears to just show up on the screen. 如果您去相册(例如Nature(自然),因为仍在处理其他照片),然后单击其中一张图像,它们全部消失,然后您单击的图像就会显示在屏幕上。 What is happening is that it is still fading in as the thumbnails are fading out. 发生的事情是,随着缩略图逐渐消失,它仍在逐渐消失。 I tried adding the rest of the code inside a .complete(), but that seems to break it. 我尝试将其余代码添加到.complete()中,但这似乎破坏了它。

$(document).ready(function(){


$('.photos').on('click', function() {

    var src = $(this).attr('src').replace('thumb-','');
    $('.photos').stop().fadeOut(500);
        $('.enlarged').remove();
        $('#album').append('<img class="enlarged" src="' + src + '">');
        $('.enlarged').hide();
        $('.enlarged').stop().fadeIn(500).done(
            $('.enlarged').on('click', function () {

                $(this).stop().fadeOut({

                    duration: 500,
                    done: this.remove()
                });

                $('.photos').stop().fadeIn(500);

            })
        );
});

});

You can use promise to catch complete all fade out animation: 您可以使用promise捕获所有淡出的动画:

$(document).ready(function(){

  $('.photos').on('click', function() {

    var src = $(this).attr('src').replace('thumb-','');
    var photos = $('.photos');
    photos.stop().fadeOut(500);

    photos.promise().done( function() {
        $('.enlarged').remove();
        $('#album').append('<img class="enlarged" src="' + src + '">');
        $('.enlarged').hide();
        $('.enlarged').stop().fadeIn(500).done(
            $('.enlarged').on('click', function () {

                $(this).stop().fadeOut({    
                    duration: 500,
                    done: this.remove()
                });

                $('.photos').stop().fadeIn(500);

            })
        );
    });

  });

});

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

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