I've made a light box with jQuery and can get the modal window to close if I select to use the body tag as the target to click and close. However, if I change it to my .closeBtn, nothing will happen. I've tried several variations of what I know to get it to work, but no luck. Any information would be most appreciated.
Cheers
// USING JQUERY CDN
$( document ).ready(function() {
$('.lightbox-trigger').click(function(e) {
e.preventDefault();
let image_href = $(this).attr("href");
if ($('#lightbox').length > 0) {
$('#content').html('<img src="' + image_href + '" />');
$('#lightbox').show('fast');
} else {
let lightbox =
'<div id="lightbox">' +
'<p class="closeBtn">×</p>' +
'<div id="content">' +
'<img class="lightbox-img" src="' + image_href +'" />' +
'</div>' +
'<a class="prev arrowBTN" aria-label="previous page">❮</a>' +
'<a class="next arrowBTN" aria-label="next page">❯</a>' +
'</div>';
$('body').append(lightbox);
}
$('.gallery-container').addClass('lightbox-active');
});
$('.closeBtn').on('click', '#lightbox', function() {
$('#lightbox').hide();
$('.gallery-container').removeClass('lightbox-active');
});
});
// END OF FUNCTION
* {
margin: 0;
padding: 0;
box-sizing: border-box;
background-color: black;
}
body {
color: white;
font-family: 'Montserrat';
}
/* ///////////////////// NAVBAR //////////////////////// */
.navbar-style {
box-shadow: 0 4px 2px -2px #333;
}
.nav-logo {
width: 10%;
}
.nav-custom img {
width: 64px;
}
.icon-bar {
background: white;
}
/* ///////////////////// NAVBAR //////////////////////// */
/* ///////////////////// MAIN AREA //////////////////////// */
.slogan h1 {
font-weight: 200;
font-size: 1.2rem;
margin-top: 2rem;
margin-left: 2rem;
}
.background-img img {
width: 80%;
}
.button {
margin-top: 2rem;
margin-left: 2rem;
background-color: black;
}
.blurb {
font-family: 'League Script', cursive;
font-weight: 100;
margin-top: 2rem;
margin-left: 2rem;
}
/* ///////////////////// FRONT GALLERY //////////////////////// */
.gallery-container {
display: flex;
justify-content: center;
border: solid 1px white;
margin: 0, auto;
}
.flex-column {
justify-content: space-between; /* have images appear as a four sided block */
max-width: 250px;
}
/* ---------- LIGHTBOX ---------- */
#lightbox {
position: fixed;
/* ^keeps lightbox window in the current viewport */
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: center;
background: rgba(0,0,0,.7);
}
/* LIGHTBOX CLOSE BUTTON */
#lightbox .closeBtn {
text-align: right;
margin-right: 20px;
font-size: 3rem;
}
.closeBtn {
cursor: pointer;
}
/* LIGHTBOX IMAGE */
#lightbox img {
box-shadow: 0 0 25px #111;
max-width: 300px;
}
#content {
display: flex;
justify-content: center;
}
#content img{
position: fixed;
top: 0;
margin-top: 4rem;
width: 100%;
}
/* BLUR BACKGROUND WHEN LIGHTBOX IS ACTIVE */
.lightbox-active {
filter: blur(5px);
}
/* LIGHTBOX ARROWS */
.arrowBTN {
position: absolute;
text-decoration: none;
color: white;
font-size: 2rem;
text-align: center;
margin-top: 25%;
transform: translateY(-25%);
width: 3rem;
height: 3rem;
border-radius: 50%;
background-color: rgba(255,255,255,0.5);
}
.prev {
left: 0;
margin-left: 1rem;
}
.next {
right: 0;
margin-right: 1rem;
}
#lightbox a {
text-decoration: none;
color: white;
}
#lightbox a:hover {
background-color: rgba(255,255,255, 0.2);
}
@media screen and (min-width: 1650px) {
.flex-column {
display: flex;
justify-content: space-evenly;
}
}
img {
margin: 5px;
}
/* ///////////////////////////////////// */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="front-gallery">
<div class="container-fluid gallery-container">
<div class="d-flex flex-row">
<!-- //////// 1st COLUMN //////// -->
<div class="d-flex flex-column">
<!-- FIRST IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2017/04/05/17/45/girl-2205813_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2017/04/05/17/45/girl-2205813_1280.jpg" alt="woman black and white photo" class="img-fluid">
</a>
<!-- SECOND IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2014/10/13/18/10/woman-487090_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2014/10/13/18/10/woman-487090_1280.jpg" alt="dog" class="img-fluid">
</a>
</div>
<!-- //////// 2nd COLUMN //////// -->
<div class="d-flex flex-column">
<!-- THIRD IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2016/11/29/13/00/black-1869685_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2016/11/29/13/00/black-1869685_1280.jpg" alt="woman color photo" class="img-fluid">
</a>
<!-- FOURTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2015/03/04/19/42/woman-659352_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2015/03/04/19/42/woman-659352_1280.jpg" alt="woman with tattoos" class="img-fluid">
</a>
</div>
<!-- //////// 3rd COLUMN //////// -->
<div class="d-flex flex-column">
<!-- FIFTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2015/07/02/10/11/person-828630_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2015/07/02/10/11/person-828630_1280.jpg" alt="outline of man photo" class="img-fluid">
</a>
<!-- SIXTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2017/05/27/17/52/model-2349037_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2017/05/27/17/52/model-2349037_1280.jpg" alt="woman under water" class="img-fluid">
</a>
</div>
<!-- //////// 4th COLUMN //////// -->
<div class="d-flex flex-column">
<!-- SEVENTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2016/02/11/19/14/couple-1194312_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2016/02/11/19/14/couple-1194312_1280.jpg" alt="couple" class="img-fluid">
</a>
<!-- EIGHTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2017/11/03/10/34/studio-2913936_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2017/11/03/10/34/studio-2913936_1280.jpg" alt="woman smiling" class="img-fluid">
</a>
</div>
<!-- //////// 5th COLUMN //////// -->
<div class="d-flex flex-column">
<!-- NINTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2016/11/14/04/08/selfie-1822563_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2016/11/14/04/08/selfie-1822563_1280.jpg" alt="kids taking selfie in field" class="img-fluid">
</a>
<!-- TENTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2021/04/02/12/01/woman-6144753_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2021/04/02/12/01/woman-6144753_1280.jpg" alt="woman with eye makeup" class="img-fluid">
</a>
</div>
<!-- //////// 6th COLUMN //////// -->
<div class="d-flex flex-column">
<!-- ELEVENTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2016/10/27/03/43/radio-1773304_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2016/10/27/03/43/radio-1773304_1280.jpg" alt="vintage radio" class="img-fluid">
</a>
<!-- TWELFTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2015/06/24/01/16/photographer-819365_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2015/06/24/01/16/photographer-819365_1280.jpg" alt="outdoor photographer" class="img-fluid">
</a>
</div>
<!-- //////// 7th COLUMN //////// -->
<div class="d-flex flex-column">
<!-- THITEENTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2015/04/12/18/05/man-719228_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2015/04/12/18/05/man-719228_1280.jpg" alt="man thinking" class="img-fluid">
</a>
<!-- FOURTEENTH IMAGE -->
<a style="display:contents" href="https://cdn.pixabay.com/photo/2016/11/29/01/34/man-1866572_1280.jpg" class="lightbox-trigger">
<img src="https://cdn.pixabay.com/photo/2016/11/29/01/34/man-1866572_1280.jpg" alt="man with jacket" class="img-fluid">
</a>
</div>
</div> <!-- END FLEX ROW -->
</div> <!-- END CONTAINER -->
</section>
<!-- ////////////////////////////// -->
<!-- <div id="lightbox2">
<p class="closeBtn2">×</p>
<div id="content2">
<img class="lightbox-img2" src="' + image_href +'" />
<img class="lightbox-img2" src="https://cdn.pixabay.com/photo/2016/11/14/04/08/selfie-1822563_1280.jpg" />
</div>
<a class="prev2 arrowBTN2" aria-label="previous page">❮</a>
<a class="next2 arrowBTN2" aria-label="next page">❯</a>
</div> -->