A Pen by Nalin Vyas on CodePen.
Last active
December 29, 2022 06:43
-
-
Save nalinvyas/36386cf80fe52232a8c2af7fda4fb509 to your computer and use it in GitHub Desktop.
Responsive Gallery with modal popup on click with HTML5, CSS3, and simple javascript
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Responsive Gallery with modal popup on click</title> | |
</head1> | |
<body> | |
<h1 class="center">Responsive Gallery with modal popup on click</h1> | |
<!-- Image Gallery --> | |
<div class="row"> | |
<img src="https://images.pexels.com/photos/1583582/pexels-photo-1583582.jpeg" alt="Image 1" class="column" onclick="openModal(1);currentSlide(1)"> | |
<img src="https://images.pexels.com/photos/3308743/pexels-photo-3308743.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" alt="Image 2" class="column" onclick="openModal(2);currentSlide(2)"> | |
<img src="https://images.pexels.com/photos/313782/pexels-photo-313782.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" alt="Image 3" class="column" onclick="openModal(3);currentSlide(3)"> | |
<img src="https://images.pexels.com/photos/13334106/pexels-photo-13334106.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" alt="Image 4" class="column" onclick="openModal(4);currentSlide(4)"> | |
</div> | |
<!-- The Modal --> | |
<div id="myModal" class="modal"> | |
<span class="close cursor" onclick="closeModal()">×</span> | |
<div class="modal-content"> | |
<div class="mySlides"> | |
<div class="numbertext">1 / 4</div> | |
<img src="https://images.pexels.com/photos/1583582/pexels-photo-1583582.jpeg" style="width:100%"> | |
</div> | |
<div class="mySlides"> | |
<div class="numbertext">2 / 4</div> | |
<img src="https://images.pexels.com/photos/3308743/pexels-photo-3308743.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width:100%"> | |
</div> | |
<div class="mySlides"> | |
<div class="numbertext">3 / 4</div> | |
<img src="https://images.pexels.com/photos/313782/pexels-photo-313782.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width:100%"> | |
</div> | |
<div class="mySlides"> | |
<div class="numbertext">4 / 4</div> | |
<img src="https://images.pexels.com/photos/13334106/pexels-photo-13334106.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" style="width:100%"> | |
</div> | |
<!-- Next/prev controls --> | |
<a class="prev" onclick="plusSlides(-1)">❮</a> | |
<a class="next" onclick="plusSlides(1)">❯</a> | |
<!-- Caption text --> | |
<div id="caption"></div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains 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
// Open the Modal | |
function openModal(n) { | |
document.getElementById("myModal").style.display = "block"; | |
showSlides((slideIndex = n)); | |
} | |
// Close the Modal | |
function closeModal() { | |
document.getElementById("myModal").style.display = "none"; | |
} | |
var slideIndex; | |
showSlides(slideIndex); | |
// Next/previous controls | |
function plusSlides(n) { | |
showSlides((slideIndex += n)); | |
} | |
// Thumbnail image controls | |
function currentSlide(n) { | |
showSlides((slideIndex = n)); | |
} | |
function showSlides(n) { | |
var i; | |
var slides = document.getElementsByClassName("mySlides"); | |
var dots = document.getElementsByClassName("demo"); | |
var captionText = document.getElementById("caption"); | |
if (n > slides.length) { | |
slideIndex = 1; | |
} | |
if (n < 1) { | |
slideIndex = slides.length; | |
} | |
for (i = 0; i < slides.length; i++) { | |
slides[i].style.display = "none"; | |
} | |
for (i = 0; i < dots.length; i++) { | |
dots[i].className = dots[i].className.replace(" active", ""); | |
} | |
slides[slideIndex - 1].style.display = "block"; | |
dots[slideIndex - 1].className += " active"; | |
captionText.innerHTML = dots[slideIndex - 1].alt; | |
} |
This file contains 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
/*heading style*/ | |
.center { | |
text-align: center; | |
color: #777; | |
font-family: sans; | |
font-weight: 400; | |
} | |
/* Modal styles */ | |
.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 */ | |
.modal-content { | |
margin: auto; | |
display: block; | |
width: 80%; | |
max-width: 700px; | |
} | |
/* 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; | |
} | |
@-webkit-keyframes zoom { | |
from { | |
-webkit-transform: scale(0); | |
} | |
to { | |
-webkit-transform: scale(1); | |
} | |
} | |
@keyframes zoom { | |
from { | |
transform: scale(0); | |
} | |
to { | |
transform: scale(1); | |
} | |
} | |
/* 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%; | |
} | |
} | |
.row { | |
display: grid; | |
grid-template-columns: repeat(2, 1fr); | |
grid-auto-rows: minmax(100px, auto); | |
grid-gap: 20px; | |
display: grid; | |
grid-template-columns: repeat( | |
2, | |
1fr | |
); /* 2 columns, each with a width of 1 fraction */ | |
padding: 0 15%; | |
flex-wrap: wrap; | |
max-width: 1000px; /* Set the maximum width of the row */ | |
margin: 0 auto; /* Center the row */ | |
} | |
.column { | |
display: flex; | |
align-items: center; | |
height: 300px; | |
/* adjust this value as needed */ | |
width: 500px; | |
margin: 10px; | |
box-shadow: 10px 5px 5px #bbb; | |
flex: 1; /* Allow the column to grow or shrink to fill the available space */ | |
min-width: 200px; /* Set the minimum width of the column */ | |
} | |
/* Make the images take up the full width of the container on small screens */ | |
@media only screen and (max-width: 1000px) { | |
.column { | |
width: 100%; | |
height: auto; | |
} | |
.row { | |
grid-template-columns: repeat(1, 1fr); | |
} | |
} | |
/* Previous button */ | |
.prev { | |
color: white; | |
position: fixed; | |
top: 50%; | |
left: 0; | |
transform: translateY(-50%); | |
} | |
/* Next button */ | |
.next { | |
color: white; | |
position: fixed; | |
top: 50%; | |
right: 0; | |
transform: translateY(-50%); | |
} | |
/* Image number on modal */ | |
.numbertext { | |
color: #f1f1f1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment