Xavier
Xavier

Reputation: 8362

.load() image loading gif

I am loading content dynamically using .load() i want to display a loading image (.gif) to show the user that something is happening.

How would i achieve this with the below code?

$('#prac_slider li a').click(function(){
    if($(this).attr('class') != 'active') {
        $('#prac_slider li a').removeClass('active').animate({ "opacity" : 0.5 });
        $(this).addClass('active').animate({ "opacity" : 1 });
        var $permalink = $(this).attr('href');
        $('#practitioner').fadeOut(1000, function(){
            $(this).load($permalink + ' #pracLoad',function(){
            $(this).fadeIn();
        })
        });
    }
    return false;
});

Upvotes: 0

Views: 2474

Answers (3)

CodeOverRide
CodeOverRide

Reputation: 4471

$.ajax({
    beforeSend: function(){
// Handle the beforeSend event
    $('#loading').show();
},
    complete: function(){
    // Handle the complete event
    $('#loading').hide();
    }
    //DO STUFF
});

Upvotes: 0

Gajendra Bang
Gajendra Bang

Reputation: 3583

add a style to your css file

.hidden {display:none;}

and now use in your jquery code as follows

$('#prac_slider li a').click(function(){
    if($(this).attr('class') != 'active') {
        $('#loadingimage').removeClass('hidden');
        $('#prac_slider li a').removeClass('active').animate({ "opacity" : 0.5 });
        $(this).addClass('active').animate({ "opacity" : 1 });
        var $permalink = $(this).attr('href');
        $('#practitioner').fadeOut(1000, function(){
            $(this).load($permalink + ' #pracLoad',function(){
            $(this).fadeIn();
        })
        $('#loadingimage').addClass('hidden');
        });
    }
    return false;
});

and finally have a image like this

<img src=""images/loading.gif" class="hidden" id="loadingimage" />

Upvotes: 0

Thorsten
Thorsten

Reputation: 5644

You might just want to do something like

$(document).ajaxStart(function(){ 
    $('#AJAXload').show(); 
}).ajaxStop(function(){ 
    $('#AJAXload').hide();
});

Upvotes: 2

Related Questions