tags:

views:

33

answers:

2

Hi trying to fade out elements on the page when a navigation link is clicked, then go to the clicked link:

$("#navigation a").click(function() {   
  var $clickobj = $this;  
  $("div#content").animate({opacity: 'toggle', paddingTop: '0px'}, 'slow',function(){  
        $("div#navigation").animate({opacity: 'toggle', paddingTop: '0px'}, 'slow', function(){  
          $("div#logo").animate({opacity: 'toggle', paddingTop: '0px'}, 900, function(){  
     $clickobj.click();       
     });  
      });  
   });  
   return false;  
 });

but this just navigates straight away with the fade out...any ideas?

A: 

It's probably doing this because $this on your first line throws an undefined error and so ti'ts not executing the script, just erroring then continuing the normal anchor behavior. The $this should just be this without the $, like this:

$("#navigation a").click(function() {   
  var $clickobj = $(this).unbind('click');
  $("div#content").animate({opacity: 'toggle', paddingTop: '0px'}, 'slow',function(){  
    $("div#navigation").animate({opacity: 'toggle', paddingTop: '0px'}, 'slow', function(){  
      $("div#logo").animate({opacity: 'toggle', paddingTop: '0px'}, 900, function(){  
        $clickobj.click();       
      });  
    });  
  });  
  return false;  
});

Also the .unbind() above removes this handler, so it won't loop.

Nick Craver
It should be `$(this)` (he calls `.click()` on it later...)
Felix Kling
@Felix - There's a native DOM click that works for this as well, it's an anchor. In this case though, he needs to unbind the click handler so it won't loop, so the jQuery version works too.
Nick Craver
@Nick Craver: Oh... didn't know that... (damn). OK, I will never try to correct you again ;)
Felix Kling
@Felix - I mess up at least 100x a day (who doesn't?), *please* **do** correct, helps the next person that finds this :)
Nick Craver
@Felix - You're right! I happened to be testing something in IE when firing jsfiddle up for this, anchor `.click()` [is IE specific](http://msdn.microsoft.com/en-us/library/ms536363.aspx) (big surprise?)
Nick Craver
A: 

The animation runs on a timer so you need to wait for that to finish. This code looks like it is going into a recursive loop, so rather call window.location once the third animation is finished:

$("#navigation a").click(function(e) {   
  var href = $(this).attr('href');
  $("div#content").animate({opacity: 'toggle', paddingTop: '0px'}, 'slow',function(){  
        $("div#navigation").animate({opacity: 'toggle', paddingTop: '0px'}, 'slow', function(){  
          $("div#logo").animate({opacity: 'toggle', paddingTop: '0px'}, 900, function(){  
     window.location = href;       
     });  
      });  
   });  
   e.preventDefault();
 });
James Westgate