Monday , February 3 2025

Popup image on click without href

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;
}

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

How to disable click on links using CSS?

Easy question, easy solution. We can disable click on all links, or we can use …

Leave a Reply

Your email address will not be published. Required fields are marked *