Reputation: 7971
how to use ajax loader is my question i am new to ajax. i have a function which work to change image when user click on thumbnail image but image are heavy so i want ajax till the second image load itself. i have created loader.gif now i just want to know how to use this image as a loader.
<script type="text/javascript">
jQuery(function (){
jQuery('#data').find('img').each(function(){
jQuery(this).click(function (){
var crd = jQuery(this).attr('title');
jQuery('#bigimg').fadeOut('slow', function (){
jQuery(this).find('#imnew').attr('src', 'images/' + crd +".jpg")
}).fadeIn()
})
})
})
</script>
HTML
<div id="bigimg">
<img src="images/bigImage1.jpg" id="imnew" alt="" />
</div>
<div id="data">
<div class="sub">
<div class="1">
<img src="images/thumb1.png" width="117" height="74" title="bigImage1" alt="" />
</div>
<div class="2">
<img src="images/thumb2.png" width="117" height="74" title="bigImage2" alt=""/>
</div>
<div class="3">
<img src="images/thumb3.png" width="117" height="74" title="bigImage3" alt=""/>
</div>
<div class="4">
<img src="images/thumb4.png" width="117" height="74" title="bigImage4" alt=""/>
</div>
<div class="5">
<img src="images/thumb1.png" width="117" height="74" title="bigImage1" alt=""/>
</div>
<div class="6">
<img src="images/thumb2.png" width="117" height="74" title="bigImage2" alt=""/>
</div>
<div class="7">
<img src="images/thumb3.png" width="117" height="74" title="bigImage3" alt=""/>
</div>
<div class="8">
<img src="images/thumb4.png" width="117" height="74" title="bigImage4" alt=""/>
</div>
</div>
</div>
Upvotes: 0
Views: 1055
Reputation: 3897
Try this:
Give all your thumbnails the same classname, and then:
$('.thumbnail').click(function (){
var thumbNail = $(this);
var thumbPath = $(this).attr('src');
var crd = $(this).attr('title');
$(this).attr('src', 'images/loader.gif');
$('#bigimg').fadeOut('slow', function (){
$(this).load(function() {
$(this).fadeIn();
$(thumbNail).attr('src', thumbPath);
});
$(this).attr('src', crd);
})
})
Upvotes: 0
Reputation: 3008
You need to use jQuery.load to tell you when the image has finished loading.
The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object. http://api.jquery.com/load/
<script type="text/javascript">
jQuery(function (){
jQuery('#data').find('img').each(function(){
jQuery(this).click(function (){
var crd = jQuery(this).attr('title');
// $('#loader').show();
jQuery('#bigimg').fadeOut('slow', function (){
jQuery(this).find('#imnew').attr('src', 'images/' + crd +".jpg")
.load(function() {
$('#loader').hide();
$('#bigimg').fadeIn()
});
})
})
})
})
</script>
Upvotes: 2