Run two jQuery functions at the same time

后端 未结 4 1853
难免孤独
难免孤独 2021-01-19 12:14

I am using jQuery to slide something down and fade something else out, but in testing it, I\'ve noticed that the fading appears too long after the sliding happens. In other

相关标签:
4条回答
  • 2021-01-19 12:52

    They should run together if you do it like:

    $('#element1').animate({
        opacity: 0.25,
        }, 1000, function() {
            // complete
    });
    
    $('#element2').animate({
        opacity: 0,
        }, 1000, function() {
            // complete
    });
    
    0 讨论(0)
  • 2021-01-19 12:53

    It'd be nicer if you set up a fiddle. If your DOM is large, you can minimally reduce the delay by doing the lookup ahead of time:

    $(document).ready(function(){
        $('#trigger').click( function(){
            var vars = { $this        : $(this),
                         $carousel    : $('#carousel'),
                         $pullrefresh : $('#pullrefresh'),
                         $detector    : $('#detector')
                       };
    
            vars.$this.animate({ opacity: 0.0 },1000);  // fade
            vars.$carousel.animate({ top: '100px' });   // slide
            vars.$pullrefresh.css('top', '-490px');     // line 5
            vars.$detector.hide();                      // line 6
        });
    });
    
    0 讨论(0)
  • 2021-01-19 13:00

    try this

     $(document).ready(function(){
            $('#trigger').click( function(){         
                $(this).animate({ opacity: 0.0 },1000);        // fade
                $('#carousel').animate({ top: '100px' }); // slide
                $('#pullrefresh').css('top', '-490px');   // line 5
                $('#detector').hide();                    // line 6
            });
        });
    

    specify the time 1000 for animate

    0 讨论(0)
  • 2021-01-19 13:02
       $(document).ready(function(){
    
           $('#trigger').click( function(){
    
               $.Deferred(function(dfr) {
    
                  dfr.pipe(function() {
                      return  $(this).animate({ opacity: 0.0 }); // fade
                  }).
                  pipe(function() {
                      return $('#carousel').animate({ top: '100px' }); // slide
                  })
                  pipe(function() {
                      return $('#pullrefresh').css('top', '-490px'); // line 5
                  }).
                  pipe(function() {
                      return $('#detector').hide(); // line 6
                  });
    
              }).resolve();
    
           });
      });
    
    0 讨论(0)
提交回复
热议问题