LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Set Image Zoom In POPUP On-Click Via Pure Vanilla JavaScript

Set Image Zoom In POPUP On-Click Via Pure Vanilla JavaScript

Set-Image-Zoom-In-POPUP-On-Click-Via-Pure-Vanilla-JavaScript
A zoom image is one of the useful features of a user-friendly webpage design. It allows users to view the image more clearly in detail. This image can be a product or a general-purpose photo. There are multiple ways to allow users to see the full image. One of them shows the full zoom image in a modal popup. So, in this tutorial, we are going to create an image zoom modal popup on click using HTML, CSS, and JavaScript.

Here we have a pure vanilla JavaScript snippet that helps you create a simple image slider with basic carousel functionalities such as infinite scrolling, autoplay, responsive and navigation with bullet points.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Recommended For You:
Add Awesome And Stylish Two Ways JQuery Slider

Features:

  1. Light Weight.
  2. Pure JavaScript Code.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Responsive.

How To Use Set Image Zoom In POPUP On-Click Via Pure Vanilla JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<style>
.thumbnail {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
display: block;
margin-left: auto;
margin-right: auto
}

.thumbnail:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.modal-content {
margin: auto;
display: block;
width: 75%;
//max-width: 75%;
}

/* Caption of Modal Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}

/* Add Animation */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}

.out {
animation-name: zoom-out;
animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
from {-webkit-transform:scale(1)}
to {-webkit-transform:scale(2)}
}

@keyframes zoom {
from {transform:scale(0.4)}
to {transform:scale(1)}
}

@keyframes zoom-out {
from {transform:scale(1)}
to {transform:scale(0)}
}

/* The Close Button */
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}

.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}
</style>

HTML:

<!-- Image Zoom HTML -->
<img class="thumbnail" src="https://codeconvey.com/Tutorials/html-image-zoom-on-click/img/image.jpg" alt="Trolltunga, Norway" width="500" height="300">
<img class="thumbnail" src="https://codeconvey.com/Tutorials/html-image-zoom-on-click/img/image.jpg" alt="Image 1" width="500" height="300">
<img class="thumbnail" src="https://codeconvey.com/Tutorials/html-image-zoom-on-click/img/image.jpg" alt="Image 2" width="500" height="300">
<img class="thumbnail" src="https://codeconvey.com/Tutorials/html-image-zoom-on-click/img/image.jpg" alt="Image 3" width="500" height="300">
<!-- The Modal -->
<div id="myModal" class="modal">
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>

JavaScript:

<script>
// Get the modal
const modal = document.getElementById('myModal');
const modalImg = document.getElementById("img01");
const captionText = document.getElementById("caption");
var img = document.querySelectorAll('.thumbnail');
for (var i=0; i<img.length; i++){
img[i].onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
}
}
// When the user clicks on <span> (x), close the modal
modal.onclick = function() {
img01.className += " out";
setTimeout(function() {
modal.style.display = "none";
img01.className = "modal-content";
}, 400);
}
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need. Remember to add JavaScript after HTML code.

Recommended For You:
Simple And Pure HTML SlideShow With Play/Pause Controls

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

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