View RSS Feed

molendijk

A close button on top of a fluid centered image

Rate this Entry
EDIT: see this for an alternative (better?) method..
CLICK HERE IF YOU WANT TO GO STRAIGHT AWAY TO THE DEMO PAGE.
The translate() method used inside the HTML-code below moves both a (super) div-containing-a-close-buttom (id: 'the_div') and an image (id: 'the_img') to the center of the window. The jquery-onload in the image ensures that the width of the div equals the width of the image. This is a requirement if we want to get the button (in the div) at the right position in front of the image (using float: right and a high zindex for the div).
The width of the image is not explicitly given, because we already have a height for it (here: 80%; fluid images require percentages) and we don't want a distorted image. The jquery-onload is capable of calculating the proper width of the div on the basis of a value for the image's width that jquery derives from the image's height. That's jquery-magic. Note that we (must) have identical values for position, size and translate for the image and the div.
Code (we must either have 'position: absolute' or 'position: fixed'; the z-index for the div must be higher than the z-index for the image):
Code:
<div id="the_div" style="position: fixed; z-index: 101; border: 1px solid black; left: 50%; top: 50%; height: 80%; max-height: 100%; -ms-transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); transform:translate(-50%, -50%); ">
<div style="background: black; color: white; font-family: verdana; font-size: 14px; font-weight: bold; float: right; cursor: pointer;" onclick="alert('Inactive close button')">&nbsp;X&nbsp;</div>
</div>
<img id="the_img" alt="" src="http://press.princeton.edu/images/k8264.gif" style="position: fixed; z-index: 100; border: 1px solid black; left: 50%; top: 50%; height: 80%; max-height: 100%; -ms-transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); transform:translate(-50%, -50%);" onload='$("#the_div").css("width", $("#the_img").width()+"px");'>
It's possible to turn this static code into one long HTML-line that creates the div (containing the close button) plus the image 'on the fly'. I added some jquery fade:
Code:
<a href="javascript: void(0)" onmouseover="if(!$('#the_div').next().length>0 ){$('body').append('<div id=\'the_div\'></div><img id=\'the_img\'>')};" onclick="$('#the_div').css('display', 'block'); $('#the_div').html('<div id=\'the_div_inner\' style=\'z-index: 101; position: fixed; left: 50%; top: 50%; height: 80%; max-height: 100%; -ms-transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); transform:translate(-50%, -50%); border: 1px solid black; display: none\'><div style=\'color: white; background: black; font-family: verdana; font-size: 14px; font-weight: bold; float: right; cursor: pointer\' onclick=\'$(&quot;#the_div&quot;).fadeOut(500); setTimeout(function() {$(&quot;#the_div&quot;).empty()}, 500);\'>&nbsp;X&nbsp;</div></div><img id=\'the_img\' src=\'http:\/\/press.princeton.edu\/images\/k8264.gif\' alt=\'\' style=\'display: none; z-index: 100; position: fixed; left: 50%; top: 50%; height: 80%; max-height: 100%; -ms-transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); transform:translate(-50%, -50%); border: 1px solid black\' onload=\'$(&quot;#the_div_inner&quot;).css(&quot;width&quot;, $(&quot;#the_img&quot;).width()+&quot;px&quot;); $(&quot;#the_div_inner&quot;).fadeIn(500); $(&quot;#the_img&quot;).fadeIn(500); \'>'); return false">show fluid centered image</a>
But we'd better use a function that does the same thing:
Code:
<script>
function create_fluid_img(which_image, which_height, which_position_type)
{
if(!$('#the_div').next().length>0 )
{$('body').append("<div id='the_div'></div><img id='the_img'>")};

$('#the_div').css('display', 'block') ;

$('#the_div').html("<div id='the_div_inner' style='z-index: 101; position:"+which_position_type+"; left: 50%; top: 50%; height:"+which_height+"%; max-height: 100%; -ms-transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); transform:translate(-50%, -50%); border: 1px solid black; display: none'><div style='color: white; background: black; font-family: verdana; font-size: 14px; font-weight: bold; float: right; cursor: pointer' onclick='$(\"#the_div\").fadeOut(500); setTimeout(function() {$(\"#the_div\").empty()}, 500);'>&nbsp;X&nbsp;</div></div><img id='the_img' src='"+which_image+"' alt='' style='display: none; z-index: 100; position:"+which_position_type+"; left: 50%; top: 50%; height:"+which_height+"%; max-height: 100%; -ms-transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); transform:translate(-50%, -50%); border: 1px solid black' onload='$(\"#the_div_inner\").css(\"width\", $(\"#the_img\").width()+\"px\"); $(\"#the_div_inner\").fadeIn(500); $(\"#the_img\").fadeIn(500); '>");

}

//Usage:
//<a onclick="create_fluid_img('http://press.princeton.edu/images/k8264.gif', '70', 'absolute')">fluid centered image, absolute position, height 70%</a>
//<a onclick="create_fluid_img('http://press.princeton.edu/images/k8264.gif', '40', 'fixed')">fluid centered image, fixed position, height 40%</a>

</script>
Demos and explanations here.

Submit "A close button on top of a fluid centered image" to del.icio.us Submit "A close button on top of a fluid centered image" to StumbleUpon Submit "A close button on top of a fluid centered image" to Google Submit "A close button on top of a fluid centered image" to Digg

Updated 04-10-2015 at 10:47 PM by molendijk

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. Beverleyh's Avatar
    Hi Arie,

    Just some feedback from Safari, Chrome, Dolphin and Mercury browsers on iOS (iPhone 5S and iPad)

    - in Mercury, the new tab/window popup does not open at all.

    - in all browsers, 2 taps are required to activate the in-page image (not a biggie - I think folks would just assume that they missed it the first time).

    - in all browsers, the image (new tab and in-page) is unfortunately not centered (the top corner is at 50%, 50%), and the [X] on the top right corner of the image is unreachable off the edge of the screen, meaning that it can't be closed.

    I'm not at a desk today so I'm unable to test further, but maybe you can take something from the centering CSS I used for my popup image viewer here http://fofwebdesign.co.uk/template/_...odal-popup.htm
    Updated 04-07-2015 at 09:07 AM by Beverleyh
  2. molendijk's Avatar
    Thanks for the feedback, Beverleyh!
    At a desktop and with my ASUS-tablet, it works on all modern browsers/WIN, Chrome/WIN included. Only one tap needed.
    Apparently, the translate() method is not supported (yet) on iOS.
    I knew about your popup image viewer, but I wanted non-modal images with a close button at the top/right
    Thanks again,
    Arie.
    ===
    EDIT: even at a desktop, it does not work with Safari/WIN. I'll see what can be done about it.
    Updated 04-07-2015 at 09:59 AM by molendijk (Correction)
  3. Beverleyh's Avatar
    Apparently, the translate() method is not supported (yet) on iOS.
    Hmmm, translate in CSS transforms works fine in iOS - I think it's down to an error in your syntax (a missing % at the end of the WebKit vendor prefix lines)
    Code:
    -webkit-transform: translate(-50%, -50);
  4. molendijk's Avatar
    Splendid! That caused the problem in Safari (and maybe on iOS, I can't check that). Thanks.
    I've corrected the error. You may have to refresh the page to see the change.
    In the meantime, I'm working on a way to do it without the translate method, just to be sure.
  5. Beverleyh's Avatar
    Yes. Centering is all fine now in the 4 popular iOS browsers mentioned earlier