Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load image with jquery?

Im doing this to load image and show image inside a dialog box.

<div id="image_preview" title="Client Photo Preview">
   <p><img src="" alt="client image" id="client_image_preview" /></p>
</div>

$("#client_image_preview").attr("src", imagelocation);

$('#image_preview').dialog({
    height: 'auto',
    width: 'auto',
    modal: true,
    dialogClass: 'titleless',
    buttons: { "CLOSE": function() { $(this).dialog("destroy"); } }
});

is there way to show a loading gif while image loads with jquery?

Regards

like image 970
Gihan Lasita Avatar asked Jan 19 '23 05:01

Gihan Lasita


1 Answers

Set an initial loading gif image in your img tag. Then preload the image using a javascript Image object and onload event; when the target image is done preloading, replace the src of your img tag with its URI.

<div id="image_preview" title="Client Photo Preview">
   <p><img src="loading.gif" alt="client image" id="client_image_preview" /></p>
</div>

var img = new Image();
img.onload = function() {
    $("#client_image_preview").attr("src", imagelocation);
}
img.src = imagelocation;

$('#image_preview').dialog({
    height: 'auto',
    width: 'auto',
    modal: true,
    dialogClass: 'titleless',
    buttons: { "CLOSE": function() { $(this).dialog("destroy"); } }
});
like image 170
joshperry Avatar answered Jan 23 '23 04:01

joshperry