tags:

views:

29

answers:

1

I have this code:

    $(document).ready(function(){
     $(".links a").click(function(e){
      var toLoad = "products.html #" + this.id;
      $('#block').fadeTo('fast',0,loadContent);

      function loadContent() {  
              $('#block').load(toLoad,'',showNewContent())  
          }  
      function showNewContent() {  
              $('#block').fadeTo('slow',100);  
          }
      e.preventDefault();
     });

    });

The idea is:

  • click on a link to trigger (check!)
  • the "block" div fades to 0 (check!)
  • the content is switched out (check!)
  • the "block" div fades back 100 (whoops!)

The behavior I see is that the div fades out, then pops back with new content as soon as the fadeOut is completed.

Any thoughts on this?

+2  A: 

Change this:

function loadContent() {
  $('#block').load(toLoad,'',showNewContent())  
}

to

function loadContent() {
  $('#block').load(toLoad,'',showNewContent)  
}

To explain: you're calling the function immediately (and passing its non-existent return as to load()) whereas you want to pass the function (rather than what it returns) as the callback.

Note: also, fadeTo() states:

The opacity to fade to (a number from 0 to 1).

so you should probably change:

function showNewContent() {  
  $('#block').fadeTo('slow',100);  
}

to

function showNewContent() {  
  $('#block').fadeTo('slow', 1);  
}
cletus
This still causes it to pop directly back. Let me put it live to demo...Also, you're 2 for 2 tonight in answering my questions. Thanks for taking a front row seat to my Sunday night jQuery self-tutorial.
Alex Mcp
You solved the bigger of two errors. (The other being that opacity is 0-1, not 0-100. It WAS fading in, but went from 0-1 (on its way to 100) VERY quickly)
Alex Mcp
Heh I was just editing that part in. :)
cletus