A very elegant solution, with which the image will not be wrapped in <a href=””></a>, image will float on click to the center of the screen and will be able to close both on the top cross and on a click on a translucent background.
Image code:
<div class="image__wrapper"> <img src="http://domainname/img.png" class="minimized" alt="" /> </div>
JS script:
<script> $(function(){ $('.minimized').click(function(event) { var i_path = $(this).attr('src'); $('body').append('<div id="overlay"></div><div id="magnify"><img src="'+i_path+'"><div id="close-popup"><i></i></div></div>'); $('#magnify').css({ left: ($(document).width() - $('#magnify').outerWidth())/2, // top: ($(document).height() - $('#magnify').outerHeight())/2 upd: 24.10.2016 top: ($(window).height() - $('#magnify').outerHeight())/2 }); $('#overlay, #magnify').fadeIn('fast'); }); $('body').on('click', '#close-popup, #overlay', function(event) { event.preventDefault(); $('#overlay, #magnify').fadeOut('fast', function() { $('#close-popup, #magnify, #overlay').remove(); }); }); }); </script>
CSS styles:
.minimized { width: 200px; cursor: pointer; border: 1px solid #FFF; } .minimized:hover { border: 1px solid yellow; } #magnify { display: none; position: fixed; max-width: 600px; height: auto; z-index: 9999; } #magnify img { width: 100%; } #overlay { display: none; background: #000; position: fixed; top: 0; left: 0; height: 100%; width: 100%; opacity: 0.5; z-index: 9990; } /* close button */ #close-popup { width: 30px; height: 30px; background: #FFFFFF; border: 1px solid #AFAFAF; border-radius: 15px; cursor: pointer; position: absolute; top: 15px; right: 15px; } #close-popup i { width: 30px; height: 30px; background: url(http://domainname/test.png) no-repeat center center; background-size: 16px 16px; display: block; } @keyframes rota { 25% { transform: rotate(360deg); } } #close-popup:hover { animation: rota 4s infinite normal; -webkit-animation-iteration-count: 1; animation-iteration-count: 1; }