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 >×</p>'
'<div id="content">'
'<img src="' image_href '" />'
'</div>'
'<a aria-label="previous page">❮</a>'
'<a 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 >×</p>
<div id="content2">
<img src="' image_href '" />
<img src="https://cdn.pixabay.com/photo/2016/11/14/04/08/selfie-1822563_1280.jpg" />
</div>
<a aria-label="previous page">❮</a>
<a aria-label="next page">❯</a>
</div> -->
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
This is not fully functional since you did not specify how you implemented the lightbox functionality (a plugin?) but that is not the question, the question is how to implement the click - here I show a static div for a lightbox inside #first-container
however, if you wished to insert one dynamically that could also be done.
I showed how to add a second one as well. I just cloned the first one with all the attached event handlers and appended it to the second container, then triggered an image that pointed to with with a data attribute- key here is the click event handler works for the close button since it is attached to the lightbox-container
class
$(function() {
$('.lightbox-trigger').on('click', function(event) {
event.preventDefault();
let imageHref = $(this).attr("href");
let targetBox = $(this).data('targetlightbox');
//console.log("tb:",targetBox);
//target or default selector set for targetBox
targetBox = targetBox ? targetBox : '.lightbox-thing:first';
console.log("tb:",targetBox);
let lightBox = $(targetBox);
lightBox.find('.lightbox-content').find('.lightbox-img').attr('src', imageHref);
lightBox.show('fast');
$('.gallery-container').addClass('lightbox-active');
});
$('.lightbox-container').on('click', '.closeBtn', function(event) {
$(event.delegateTarget).find('.lightbox-thing').hide(); // the lightbox
$('.gallery-container.lightbox-active').removeClass('lightbox-active');
});
/* add a second lightbox - by cloning first one */
/* rather than embed in script, I just cloned the prior one here but it IS dynamic */
let newBox = $('.lightbox-container:first').find('.lightbox-thing').clone(true);
newBox.addClass('new-lightbox');
// console.log("nb:",newBox.length);
// console.log(newBox.attr('class'));
$('#second-container').append(newBox);
// console.log("appended:",$(".lightbox-thing").last().attr('class'), $('.lightbox-trigger').last().data('xx'));
$('.lightbox-trigger').last().trigger('click');//trigger click on last which has a target
// $('.lightbox-trigger').eq(2).trigger('click');
});
* {
margin: 0;
padding: 0;
box-sizing: border-box;
background-color: black;
}
body {
color: white;
font-family: 'Montserrat';
}
.icon-bar {
background: white;
}
.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;
}
.gallery-container {
display: flex;
justify-content: center;
border: solid 1px white;
margin: 0, auto;
}
.flex-column {
justify-content: space-between;
max-width: 250px;
}
.lightbox-container {
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);
border: solid yellow 1px;
}
.lightbox-container .closeBtn {
text-align: right;
margin-right: 20px;
font-size: 3rem;
}
.closeBtn {
cursor: pointer;
}
.lightbox-container img {
box-shadow: 0 0 25px #111;
max-width: 300px;
}
.lightbox-content {
display: flex;
justify-content: center;
}
.lightbox-container .lightbox-content img {
position: fixed;
top: 0;
margin-top: 4rem;
width: 100%;
}
.lightbox-active {
filter: blur(5px);
}
.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-container a {
text-decoration: none;
color: white;
}
.lightbox-container 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">
<div class="d-flex flex-column">
<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>
<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>
<div class="d-flex flex-column">
<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>
<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>
<div class="d-flex flex-column">
<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>
<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>
<div class="d-flex flex-column">
<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>
<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>
<div class="d-flex flex-column">
<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>
<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>
<div class="d-flex flex-column">
<a style="display:contents" href="https://cdn.pixabay.com/photo/2016/10/27/03/43/radio-1773304_1280.jpg" class="lightbox-trigger" data-targetlightbox=".new-lightbox">
<img src="https://cdn.pixabay.com/photo/2016/10/27/03/43/radio-1773304_1280.jpg" alt="vintage radio" class="img-fluid">
</a>
<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>
<div class="d-flex flex-column">
<a style="display:contents" href="https://cdn.pixabay.com/photo/2015/04/12/18/05/man-719228_1280.jpg" class="lightbox-trigger" data-targetlightbox=".new-lightbox">
<img src="https://cdn.pixabay.com/photo/2015/04/12/18/05/man-719228_1280.jpg" alt="man thinking" class="img-fluid">
</a>
<a style="display:contents" href="https://cdn.pixabay.com/photo/2016/11/29/01/34/man-1866572_1280.jpg" class="lightbox-trigger" data-targetlightbox=".new-lightbox">
<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>
</div>
</section>
<div id="first-container" class="lightbox-container">
<div class="lightbox-thing">
<p class="closeBtn">×</p>
<div class="lightbox-content">
<img class="lightbox-img" src="#" />
</div>
<a class="prev arrowBTN" aria-label="previous page">❮</a>
<a class="next arrowBTN" aria-label="next page">❯</a>
</div>
</div>
<div id="second-container" class="lightbox-container">
</div>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
This is because, you are trying to click a button that is generated dynamically. Try this,
$('body').on('click', '#lightbox .closeBtn', function() {
$('#lightbox').hide();
$('.gallery-container').removeClass('lightbox-active');
});
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>