Skip to content

Instantly share code, notes, and snippets.

@Martydesign
Created June 19, 2018 15:49
Show Gist options
  • Save Martydesign/bff4047d2569faff96beb7dc530e4365 to your computer and use it in GitHub Desktop.
Save Martydesign/bff4047d2569faff96beb7dc530e4365 to your computer and use it in GitHub Desktop.
Overlay for gallery image using jQuery
var gallery = $('.gallery'),
startingOpacity = gallery.find('img').css('opacity');
// animate opacity on hover
gallery.find('img').on('mouseenter mouseleave', function(event){
var opacity = event.type === 'mouseenter' ? 1 : startingOpacity;
$(this).fadeTo(200, opacity);
});
// make the overlay
var overlay = $('<div>', { id: 'overlay' });
overlay.appendTo('body').hide();
overlay.on('click', function() {
$(this).fadeOut('fast');
});
// hide overlay on escape
$(document).on('keyup', function(event) {
if ( event.which === 27 ) overlay.fadeOut('fast');
});
// after click show lightbox
gallery.find('a').on('click', function(event)
{
var href = $(this).attr('href'),
image = $('<img>', { src: href, attr: 'learn2code' });
overlay.html( image )
.show();
event.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment