Skip to content

Instantly share code, notes, and snippets.

@nandoflorestan
Created March 31, 2012 11:38
Show Gist options
  • Save nandoflorestan/2262590 to your computer and use it in GitHub Desktop.
Save nandoflorestan/2262590 to your computer and use it in GitHub Desktop.
jQuery: Click an image to show it big on a viewport-sized modal overlay
<html>
<head>
<title>jQuery: Click an image to show it big on a viewport-sized modal overlay
</title>
</head>
<body>
<p>
<img class="zoomable" style='width:40%;' src="http://www.tumblr.com/photo/1280/artedaalma/19707693049/1/tumblr_m19h2mwFRQ1rsn8cy" />
</p>
<p>
<img class="zoomable" style='width:40%;' src="http://www.tumblr.com/photo/1280/artedaalma/19707693049/1/tumblr_m19h2mwFRQ1rsn8cy" />
</p>
<script src="http://static.tumblr.com/xz44nnc/o5lkyivqw/jquery-1.3.2.min.js"></script>
<script>
$('img.zoomable').css({cursor: 'pointer'}).live('click', function () {
var img = $(this);
var bigImg = $('<img />').css({
'max-width': '100%',
'max-height': '100%',
'display': 'inline'
});
bigImg.attr({
src: img.attr('src'),
alt: img.attr('alt'),
title: img.attr('title')
});
var over = $('<div />').text(' ').css({
'height': '100%',
'width': '100%',
'background': 'rgba(0,0,0,.82)',
'position': 'fixed',
'top': 0,
'left': 0,
'opacity': 0.0,
'cursor': 'pointer',
'z-index': 9999,
'text-align': 'center'
}).append(bigImg).bind('click', function () {
$(this).fadeOut(300, function () {
$(this).remove();
});
}).insertAfter(this).animate({
'opacity': 1
}, 300);
});
</script>
</body>
</html>
@asmirbe
Copy link

asmirbe commented Sep 17, 2020

Hello, how to close the image on scroll ?

@nandoflorestan
Copy link
Author

I will leave that as an exercise to the reader, since the above code was posted 9 years ago and I have been avoiding jQuery as much as possible in the past few years.

@asmirbe
Copy link

asmirbe commented Sep 17, 2020

Hey, again okey so i guess i need to found out myself thank you anyway your code is working fine :)

@hatecko23
Copy link

Thank you for saving my time ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment