Skip to content

Instantly share code, notes, and snippets.

@lutsen
Last active January 17, 2017 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lutsen/7337852 to your computer and use it in GitHub Desktop.
Save lutsen/7337852 to your computer and use it in GitHub Desktop.
// Image popover
// http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/super-simple-lightbox-with-css-and-jquery/
jQuery(document).ready(function($) {
$('.lightbox_trigger').click(function(e) {
//prevent default action (hyperlink)
e.preventDefault();
//Get clicked link href
var image_href = $(this).attr("href");
var flickr_link = $(this).data("flickr");
/*
If the lightbox window HTML already exists in document,
change the img src to to match the href of whatever link was clicked
If the lightbox window HTML doesn't exists, create it and insert it.
(This will only happen the first time around)
*/
if ($('#lightbox').length > 0) { // #lightbox exists
//place href as img src value
$('#lightbox').find('img').attr('src', image_href);
$('#lightbox').find('a').attr('href', flickr_link);
//show lightbox window - you could use .show('fast') for a transition
$('#lightbox').show();
}
else { //#lightbox does not exist - create and insert (runs 1st time only)
//create HTML markup for lightbox window
var lightbox =
'<div id="lightbox">' +
'<div>' + //insert clicked link's href into img src
'<img src="' + image_href +'" height="100%" />' +
'</div>' +
'<p><a href="'+flickr_link+'" target="_blank" class="more">View on Flickr</a></p>' +
'</div>';
//insert lightbox HTML into page
$('body').append(lightbox);
$('#lightbox').click(function() {
$('#lightbox').hide();
});
}
});
});
<a href="http://farm6.staticflickr.com/5545/10474193904_ea2bf46227_o.png" data-flickr="http://www.flickr.com/photos/36919433@N08/10474193904" class="lightbox_trigger">
<img src="http://farm6.staticflickr.com/5545/10474193904_ea2bf46227_o.png" width="100%" />
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment