Hide images until they're loaded

前端 未结 2 1091
终归单人心
终归单人心 2020-12-18 16:01

I got a jQuery script which is dinamically appending data to the \"holder\" at some timeinterval. The data looks like this:

相关标签:
2条回答
  • 2020-12-18 16:23

    just set the onload property when you add the image.

    var img = new Image();
    img.src = "some url"
    img.onload=function(){$(img).fadeIn(500);}
    document.getElementByID('parent').appendChild(img);
    

    see working example here

    0 讨论(0)
  • 2020-12-18 16:25

    You can add your images in first-loaded-first displayed order like this:

    Demo: http://jsfiddle.net/m1erickson/7w6cb/

    var imageURLs=[];
    var imgs=[];
    imageURLs.push("house100x100.png");
    imageURLs.push("house32x32.png");
    imageURLs.push("house16x16.png");
    
    for(var i=0;i<imageURLs.length;i++){
    
        imgs.push(document.createElement("img"));
        imgs[i].onload=function(){
            var id=this.myId;
            this.id=id;           
            document.body.appendChild(this);
            $("#"+id).hide().fadeIn(1500);
        }
        imgs[i].myId=i;
        imgs[i].src=imageURLs[i];
    }
    
    0 讨论(0)
提交回复
热议问题