How do I chain or queue custom functions using JQuery?

前端 未结 15 1459
暗喜
暗喜 2020-12-04 19:49

I have multiple functions the do different animations to different parts of the HTML. I would like to chain or queue these functions so they will run the animations sequenti

15条回答
  •  我在风中等你
    2020-12-04 20:35

    @TrippRitter gotta attach .call to the callback

    One = function(callback){
        $('div#animateTest1').animate({ left: '+=200' }, 2000, function(){
            if(typeof callback == 'function'){
                callback.call(this);
            }
        });
    }
    
    Two = function(){
        $('div#animateTest2').animate({ width: '+=200' }, 2000);
    }
    
    One(function(){ 
        Two();
    });
    

    but its is the same as doing the following

    $('div#animateTest1')
        .animate({ left: '+=200' }, 2000, 
        function(){
           Two();
        });
    
    Two = function(){
        $('div#animateTest2').animate({ width: '+=200' }, 2000);
    }
    

提交回复
热议问题