Load page only when image is loaded

Asked

Viewed 3,033 times

3

Load page only when image is loaded, as I do using JQuery?

I tried some things but unsuccessfully so far, someone helps me...

I don’t have the only image id of the div q it is inside...

EDIT: I’m using a jquery plugin for background image, the backstrech and this background image is 2mb and it takes time to load, the code I use is this:

<script src="jquery.backstretch.min.js"></script>
<script>
    $.backstretch("fundo.gif");
</script>

backstretch uses div with class backstretch

I tried from there several ways to do this guy:

<script>
$(".backstretch").load(function() {
    $("#corpo").show();
});
</script>

and this:

<script>
$("img.backstretch").load(function() {
    $("#corpo").show();
});
</script>
  • inform you a little bit more like your html snippet here to make it easier for us to help you

  • put more information Rogerio, hope it helps...

  • Do the following, make sure there are no errors happening in your javascript that is interrupting the execution of it. Then put a alert within the load functions and see if they are being called. Break at last make a test and call by the function $('#corpo').show(); and see if the result is as expected.

  • actually there is no error in the script, simply it works but the image loads after anyway...

  • Next, put in your CSS, stating that the image and the #body start invisible (display:None / visibility:Hidden). Put a summary HTML page so I understand better :).

  • Recalling that the show will only take effect if your element #corpo be with display: none. If you’re with visibility: hidden - which means that it is invisible, but still occupies space on the page - the show does not affect it, it would be necessary to use .css("visibility", "visible") in that case.

Show 1 more comment

3 answers

2

Use the image LOAD event in question

$('#IDdaImagem').on('load',function(){ 
    //'Código para carregar a página/conteúdo' 
})

Img inside a DIV

$('#IDdaDIV img').on('load',function(){ 
    //'Código para carregar a página/conteúdo' 
})
  • I don’t have the only image id of the div q it is inside...

  • @user3230262 no problem, Voce can inform by the image element inside the X div.

0

Try this:

    var img = document.getElementsByTagName('img')[0];        
    if (!img.complete) {
           img.src = "../imagens/sem_imagem.jpg" ;
     }

EDIT: obviously I didn’t use jquery, because it’s much easier

0

I advise you to use it this way:

<script src="http//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
    $(window).load(function(){
        $('body').css('display','block');
    });
</script>

From display None on body, when the page is 100% loaded the script makes it appear, you can use those loading gifs etc.

  • but the page loads and the image does not

  • the image I need has 2mb and it never works that preloading scheme

  • hmm, have you tried using resize libraries? can help you in any way, otherwise your page will take time to load by the visa

  • is a 2mb animated gif, which resize libraries ?

  • Google has one, a one searched, I develop in Mangento to outside the libraries, own Mangento already has his, he picks up a heavy image and writes to the size I want, generating a new image cached making much faster the image loading

Browser other questions tagged

You are not signed in. Login or sign up in order to post.