jQuery event order and waiting till animation complete

前端 未结 2 1241
南笙
南笙 2020-12-17 00:42

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?

相关标签:
2条回答
  • 2020-12-17 01:08

    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.
      });
    });
    
    0 讨论(0)
  • 2020-12-17 01:10

    @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;
         }
       });
    });
    
    0 讨论(0)
提交回复
热议问题