javascript - Show "loading" while a new image is loading - Stack Overflow

I have <img src="..." class="myImage"><a href="..." class="

I have

<img src="..." class="myImage">
<a href="..." class="changeImage">

$(function(){
   $('a.changeImage').click(function(){
       $('img.myImage').attr('src',NEWVALUE);
   });
});

When I click the link, i inject a new src to .myImage. Is it possibile to show a loading image while the new src is loading?

I have

<img src="..." class="myImage">
<a href="..." class="changeImage">

$(function(){
   $('a.changeImage').click(function(){
       $('img.myImage').attr('src',NEWVALUE);
   });
});

When I click the link, i inject a new src to .myImage. Is it possibile to show a loading image while the new src is loading?

Share Improve this question asked Mar 6, 2012 at 10:53 dynamicdynamic 48.1k56 gold badges161 silver badges236 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Yes, you should use image preloading

var newImage = new Image();
var loadingSrc = 'loading.gif';
$('img.myImage').attr('src', loadingSrc);
newImage.onload = function() {
    $('img.myImage').attr('src', newImage.src);
};
newImage.src = NEWVALUE;
$(function(){
   $('a.changeImage').click(function(e){
       e.preventDefault();
       //show the loading div
       $('img.myImage').attr('src',NEWVALUE);
       $("img").trigger("onload");
   });

$("img").bind("onload",function(){

 //remove the loading div
});
});

http://jsfiddle/PrXSW/9/

You can use the load event (untested code)

<img src="smallLoader.png" alt="loading" id="loading" />
<img alt="bigImage" id="bigImage" style="display:none" />

<script type="text/javascript">
$(document).ready(function() {

    $('#bigImage').load(function() {
        $('#loading').hide();
        $('#bigImage').show();
    });

    $('#bigImage').attr("src", "bigimage.png");
});
</script>

Take a look to the jquery.blockui plugin. It may fit your needing.

the simplest solution would be to declare the loading img via CSS as background-image.

#loaded-image {
    /* shows loading icon until picture is fully loaded */
    background-image: url(loading.gif);
    background-repeat: no-repeat;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744294377a4567189.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信