jQuery event order and waiting till animation complete

[亡魂溺海] 提交于 2019-12-29 06:21:30

问题


I have an slider animation but on clX.click event #close div hides before it is animated -250px left. How to wait till the animation completes and then hide #close div?

    $(document).ready(function() {
        $("#open").click(function() {
            if ($("#close").is(":hidden")) {
                $("#open").animate({
                    marginLeft: "-32px"
                }, 200);

                $("#close").show();
                $("#close").animate({
                    marginLeft: "250px"
                }, 500);
            }
        });
        $("#clX").click(function() {
            $("#close").animate({
                marginLeft: "-250px"
            }, 500);

            $("#open").animate({
                marginLeft: "0px"
            }, 200);

            $("#close").hide();
        });
    });

回答1:


You can add a callback function to the animation. It would be fired once the animation is finished.

$('#clX').click(function() {
  $('#close').animate({
    marginLeft: "-250px"
  }, 500, function() {
    // Animation complete.
    $("#close").hide();
    //i supose $this.hide() <br/>would work also and it is more efficient.
  });
});



回答2:


@hasan, methinks @patxi meant $(this)

var closeable = $('#close');
$('#clx').bind('click', function(){
   // $(this) === $('#clx')
   closeable.stop().animate({marginLeft:'-250px'},{
     duration: 500,
     complete: function(){
        $(this).hide(); 
        // $(this) === closeable;
     }
   });
});


来源:https://stackoverflow.com/questions/2407592/jquery-event-order-and-waiting-till-animation-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!