Search code examples
javascriptjqueryjquery-chaining

nice jquery load effect


I would like to load a page inside a div with fadeTo effects. I have created this function for that

function show(div) {
$("#showdata").fadeTo(300,0.0,function() {
                $("#showdata")
                .html('<img src="images/loading.gif" />')
                .load('includes/show/'+div+'.inc.php')
                .fadeTo(300,1.0);;
            }); 
}

The fade from current contents to the image is ok,but after that ,the new contents pop all of a sudden .

I also tried this,but same results :

function show(div) {
$("#showdata").fadeTo(300,0.0,function() {
                $("#showdata")
                .html('<img src="images/loading.gif" />')
                .load('includes/show/'+div+'.inc.php',$("#showdata").fadeTo(300,1.0));
            }); 
}

Solution

  • You need to wrap your code for the .load() callback as an anonymous function like this:

    function show(div) {
      $("#showdata").fadeTo(300,0.0,function() {
        $("#showdata").html('<img src="images/loading.gif" />')
                      .load('includes/show/'+div+'.inc.php', function() {
                         $(this).fadeTo(300,1.0)
                      });
      }); 
    }