Note that there are some explanatory texts on larger screens.

plurals
  1. POsame calculation functions, different outcomes depending on .load() and .resize() or .scroll()
    primarykey
    data
    text
    <p>I'm trying to write my own lightbox script but I'm stuck on a problem.</p> <p>The <code>wrapper</code> div centering is done through <code>position: absolute</code> and top / left positioned by calculating...</p> <p>top:</p> <pre><code>_center_vertical = function() { return (($(window).height() - wrapper.height()) / 2) - (options.margin + options.border) + $(window).scrollTop() } </code></pre> <p>left:</p> <pre><code>_center_horizontal = function() { return (($(window).width() - wrapper.width()) / 2) - (options.margin + options.border) + $(window).scrollLeft() } </code></pre> <p>The <code>wrapper</code> div is centered on <code>.load()</code> and on <code>$(window).resize()</code> / <code>$(window).scroll()</code>.</p> <p>When the image is loaded and appended to <code>wrapper</code>, top and left is calculated using the functions above, horizontal centering is correct, but vertical centering is not. It is off by around 10px or more.</p> <p>When the browser window is resized or scrolled, it calls the function which animates the centering which uses the same function to calculate the top and left. The window resize / scroll does center the image properly.</p> <p>I have tried using jQuery deferred.then() to have it calculate the top / left after the image is appended, but it didn't change anything.</p> <p>Example: <a href="http://jsfiddle.net/vfMNQ/" rel="nofollow">http://jsfiddle.net/vfMNQ/</a></p> <hr> <p>I initially thought that the difference in top position changed when I played around with things like wrapper padding (aka my border), however, I found that I was wrong.</p> <p>I added some <code>console.log('image load height: ' + ((($(window).height() - wrapper.height()) / 2) - (options.margin + options.border)) + 'px')</code> to <code>.load()</code> and <code>.scroll()</code> and found that the difference was oddly 21px no matter what. The default border is 10px, margin is 30... so where did the 21 come from?</p> <p>I'd hate to use <code>+ 21</code> as a hack, but seems like nobody can figure it out.</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