Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving width() and height()information from <img> loaded through html()
    primarykey
    data
    text
    <p>My problem is the following: I developed a script that allows an user to zoom over an image displayed on the current page. On click, it loads through <code>$('div#imageBox').html()</code> a <code>&lt;div&gt;</code> and an <code>&lt;img&gt;</code>. On the following, I'm retrieving pieces of information such as the <code>width</code> and the <code>height</code> of the loaded image.</p> <p>Sometimes it works, sometimes it doesn't. I know there is a DOM-loading issue but the weird thing is that I can't figure out why sometimes it works and it doesn't. It loads a <code>&lt;div&gt;</code> of <code>0x19px</code> size because of an <code>&lt;img&gt;</code> of <code>0x0</code>.</p> <p>Here is my jQuery code :</p> <pre><code>function imageBox(src, legende) { $('DIV#imageBox').empty().fadeIn().html('&lt;div&gt;&lt;a href="'+src+'" target="_blank"&gt;&lt;img src="'+src+'" /&gt;&lt;/a&gt;&lt;/div&gt;'); var marginLeft = $('DIV#imageBox DIV').width() / 2; var marginTop = $('DIV#imageBox DIV').height() / 2; var hauteur = $('DIV#imageBox IMG').height(); var largeur = $('DIV#imageBox IMG').width(); var bottom = $('DIV#imageBox SPAN').height(); //alert(marginLeft+'/'+marginTop+'/'+hauteur+'/'+largeur+'/'+bottom); if(legende) $('DIV#imageBox').find('DIV').append('&lt;span&gt;'+legende+'&lt;/span&gt;'); if(largeur &gt; $(window).width()) largeur = $(window).width() * 0.8; if(hauteur &gt; $(window).height()) hauteur = $(window).height() * 0.8; // Récupération des nouvelles valeurs au cas où l'image soit redimensionné car trop grande $('DIV#imageBox IMG').hide().height(hauteur); largeur = $('DIV#imageBox IMG').width(); marginTop = hauteur / 2; marginLeft = largeur / 2; if(!$('DIV#imageBox IMG').is(':animated')) { $('DIV#imageBox IMG').show().height(0).animate({'height': hauteur}, 300); $('DIV#imageBox SPAN').width(0).animate({'width': largeur-10}, 300); } $('DIV#imageBox DIV').css({ 'top': '50%', 'left': '50%', 'marginTop': '-'+(marginTop)+'px', 'marginLeft': '-'+(marginLeft)+'px' }); $('DIV#imageBox SPAN').css('bottom', '-'+bottom+'px'); $('DIV#imageBox SPAN:before').css('top', hauteur); } </code></pre> <p>Should you have any questions, don't hesitate. I might not have been as clear as I hoped.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload