Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery image size calculation doesn't work in Chrome / Safari
    primarykey
    data
    text
    <p><strong>IDEA</strong></p> <p>A page with a few div elements, some are visible, some must remain unseen until particular span element is clicked. One of those invisible elements is a container for several images with pop-up text, which should be placed randomly on page but with <code>20px</code> margin from every edge of the page (page is set to <code>overflow:hidden;</code>).</p> <p><strong>HTML</strong> (including jQuery 1.8.3, jQuery UI 1.10.1 and jQuery UI touch-punch):</p> <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;body&gt; &lt;div class="pattern" style="background-image: url('m.png');"&gt;&lt;/div&gt; &lt;div class="shadow pat-main-block"&gt; &lt;span class="mood-board"&gt;Show container&lt;/span&gt; &lt;/div&gt; &lt;!-- Container div --&gt; &lt;div id="mood-board"&gt; &lt;span class="hide-mood shadow"&gt;Hide container&lt;/span&gt; &lt;div id="img1" class="drag mood-img"&gt; &lt;img id="mimg1" class="shadow" src="mood/brick-mood-1.png"&gt; &lt;span class="mood-name shadow"&gt;text&lt;/span&gt; &lt;/div&gt; &lt;div id="img2" class="drag mood-img"&gt; &lt;img id="mimg2" class="shadow" src="mood/brick-mood-4.png"&gt; &lt;span class="mood-name shadow"&gt;text&lt;/span&gt; &lt;/div&gt; &lt;div id="img3" class="drag mood-img"&gt; &lt;img id="mimg3" class="shadow" src="mood/brick-mood-3.png"&gt; &lt;span class="mood-name shadow"&gt;text&lt;/span&gt; &lt;/div&gt; &lt;div id="img4" class="drag mood-img"&gt; &lt;img id="mimg4" class="shadow" src="mood/brick-mood-2.png"&gt; &lt;span class="mood-name shadow"&gt;text&lt;/span&gt; &lt;/div&gt; &lt;div id="img5" class="drag mood-img"&gt; &lt;img id="mimg5" class="shadow" src="mood/brick-draw-4.png"&gt; &lt;span class="mood-name shadow"&gt;text&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- End of the container div --&gt; &lt;/body&gt; </code></pre> <p></p> <p>This code is simplified — I removed the elements not affecting the case and left only 5 elements in container div (actually there are 13 elements). I want to make a kind of template so it could be used with different images and different amount of images (elements).</p> <p><strong>CSS:</strong></p> <pre><code>html, body { width:100vw; height:100vh; margin:0; padding:0; overflow:hidden; } .pattern { width:100%; height:100%; margin:0; padding:0; position:absolute; } .pat-main-block { position:fixed; top:100px; left: 100px; background: #fff; padding: 40px; } #mood-board { position: absolute; width:100%; height:100%; margin-top:-1000px; left:0; } .mood-img {position:absolute;} .mood-img:hover .mood-name {opacity:1;} .mood-name { position:absolute; opacity:0; background:#fff; bottom:15px; right:15px; } .hide-mood { position:absolute; top:40px; left:40px; padding:9px; background:#1900DC; color:#fff; z-index:99; cursor:pointer; } </code></pre> <p><strong>JS 1:</strong></p> <pre><code>$(function(){ $(".mood-board").click(function(){ $("#mood-board").css("display", "block"); }); }); </code></pre> <p><strong>JS 2:</strong></p> <pre><code>$(document).ready(function () { function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } $("#img1").each(function () { var maxtop = $('.pattern').outerHeight() - $('#mimg1').outerHeight() - 20, maxleft = $('.pattern').outerWidth() - $('#mimg1').outerWidth() - 20, randomtop = getRandomInt(20, maxtop), randomleft = getRandomInt(20, maxleft), randomzindex = getRandomInt(1, 30); alert ( $('#mimg1').outerHeight() ); $("#img1").css({ "margin-top": randomtop, "margin-left": randomleft, "z-index": randomzindex }); }); $("#img2").each(function () { var maxtop = $('.pattern').outerHeight() - $('#mimg2').outerHeight() - 20, maxleft = $('.pattern').outerWidth() - $('#mimg2').outerWidth() - 20, randomtop = getRandomInt(20, maxtop), randomleft = getRandomInt(20, maxleft), randomzindex = getRandomInt(1, 30); $("#img2").css({ "margin-top": randomtop, "margin-left": randomleft, "z-index": randomzindex }); }); $("#img3").each(function () { var maxtop = $('.pattern').outerHeight() - $('#mimg3').outerHeight() - 20, maxleft = $('.pattern').outerWidth() - $('#mimg3').outerWidth() - 20, randomtop = getRandomInt(20, maxtop), randomleft = getRandomInt(20, maxleft), randomzindex = getRandomInt(1, 30); $("#img3").css({ "margin-top": randomtop, "margin-left": randomleft, "z-index": randomzindex }); }); $("#img4").each(function () { var maxtop = $('.pattern').outerHeight() - $('#mimg4').outerHeight() - 20, maxleft = $('.pattern').outerWidth() - $('#mimg4').outerWidth() - 20, randomtop = getRandomInt(20, maxtop), randomleft = getRandomInt(20, maxleft), randomzindex = getRandomInt(1, 30); $("#img4").css({ "margin-top": randomtop, "margin-left": randomleft, "z-index": randomzindex }); }); $("#img5").each(function () { var maxtop = $('.pattern').outerHeight() - $('#mimg5').outerHeight() - 20, maxleft = $('.pattern').outerWidth() - $('#mimg5').outerWidth() - 20, randomtop = getRandomInt(20, maxtop), randomleft = getRandomInt(20, maxleft), randomzindex = getRandomInt(1, 30); $("#img5").css({ "margin-top": randomtop, "margin-left": randomleft, "z-index": randomzindex }); }); $("#mood-board").css({ "display": "none", "margin-top": "0" }); }); </code></pre> <p>The method I'm trying to use is to calculate page's width and height, width and height of the image, then to calculate minimum and maximum range for random positioning (margin-top and margin-left or just top and left), so that the margin between the edge of the image and the edge of the page won't be less the 20 pixels. </p> <p>Container with images is positioned off the screen, then after images are loaded and calculated, container is made invisible and put to fit the screen. In Firefox it works fine, alerts show the correct calculation results, but in Chrome and Safari image sizes are seen as 0 and the final calculations are wrong making some images go behind the page's edges.</p> <p>The other thing I would like to do is to make a js-code universal, so there would be no need to mention every #img and #mimg elements.</p> <p><a href="http://thelocalgenius.com/patterns/brickwork-classics/" rel="nofollow">http://thelocalgenius.com/patterns/brickwork-classics/</a></p> <p><strong>The same code works fine in JSfiddle:</strong> <a href="http://jsfiddle.net/n3q92/1/" rel="nofollow">http://jsfiddle.net/n3q92/1/</a> Although it's almost the same code that doesn't work on my page, and those parts that couldn't be included to JSfiddle don't seem to be the reason — I've tried to disable them all and it didn't affect how the page works.</p> <p><strong>UPDATE:</strong> Fiddle works in Chrome, but not in Safari.</p> <p><strong>UPDATE 2:</strong> I've found one more interesting thing — in Firefox if the page loads without cache, script can't get image's width and height the same way as in Chrome and Safari. I don't understand why because the images supposed to be loaded since they are in a div with <code>display:block;</code> option just positioned 1000px above the screen edge. Anybody has any ideas? </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.
    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