Created
June 19, 2018 15:49
-
-
Save Martydesign/bff4047d2569faff96beb7dc530e4365 to your computer and use it in GitHub Desktop.
Overlay for gallery image using jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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