Hello guys so im trying to make model image function for my gallery but I can’t use the function on many image, any suggestion on how fix it ? can I change the var img to collect the element by class name? Thanks before.
Here’s the code:
JavaScript
x
17
17
1
<div id="myModal" class="modal">
2
<span class="close">×</span>
3
<img class="modal-content" id="img01">
4
</div>
5
6
<div class="container-fluid padding gallerysection text-center" style="padding-bottom:30px;">
7
<div class="row padding fadeineffect">
8
<div class="col-lg-6">
9
<img id="modalImg" src="img/gallery/MR4.jpg" alt="MR Cello 1" class="galleryphoto img-fluid">
10
</div>
11
12
<div class="col-lg-6">
13
<img src="img/gallery/MR5.jpg" alt="MR Cello 2" class="galleryphoto img-fluid">
14
</div>
15
</div>
16
</div>
17
My CSS Code:
JavaScript
1
23
23
1
/* The Modal (background) */
2
.modal {
3
display: none; /* Hidden by default */
4
position: fixed; /* Stay in place */
5
z-index: 1; /* Sit on top */
6
padding-top: 100px; /* Location of the box */
7
left: 0;
8
top: 0;
9
width: 100%; /* Full width */
10
height: 100%; /* Full height */
11
overflow: auto; /* Enable scroll if needed */
12
background-color: rgb(0,0,0); /* Fallback color */
13
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
14
}
15
16
/* Modal Content (image) */
17
.modal-content {
18
margin: auto;
19
display: block;
20
width: 100%;
21
max-width: 850px;
22
}
23
My Java script Code:
JavaScript
1
20
20
1
// Get the modal
2
var modal = document.getElementById("myModal");
3
4
// Get the image and insert it inside the modal - use its "alt" text as a caption
5
var img = document.getElementById("modalImg");
6
var modalImg = document.getElementById("img01");
7
img.onclick = function(){
8
modal.style.display = "block";
9
modalImg.src = this.src;
10
}
11
12
// Get the <span> element that closes the modal
13
var span = document.getElementsByClassName("close")[0];
14
15
// When the user clicks on <span> (x), close the modal
16
span.onclick = function() {
17
modal.style.display = "none";
18
}
19
20
Advertisement
Answer
Working with multiple images means working with a collection. For collection work, method forEach
works well:
JavaScript
1
7
1
[].forEach.call(img, function (img_curr) {
2
img_curr.onclick = function () {
3
modal.style.display = "block";
4
modalImg.src = this.src;
5
};
6
});
7
img_curr
– the current image from the collection.
Use class modalImg
for all img
tag.
JavaScript
1
17
17
1
var modal = document.getElementById("myModal");
2
3
var img = document.getElementsByClassName("modalImg");
4
var modalImg = document.getElementById("img");
5
6
[].forEach.call(img, function (img_curr) {
7
img_curr.onclick = function () {
8
modal.style.display = "block";
9
modalImg.src = this.src;
10
};
11
});
12
13
var span = document.getElementsByClassName("close")[0];
14
15
span.onclick = function () {
16
modal.style.display = "none";
17
};
JavaScript
1
20
20
1
.modal {
2
display: none;
3
position: fixed;
4
z-index: 1;
5
padding-top: 100px;
6
left: 0;
7
top: 0;
8
width: 100%;
9
height: 100%;
10
overflow: auto;
11
background-color: rgb(0, 0, 0);
12
background-color: rgba(0, 0, 0, 0.9);
13
}
14
15
.modal-content {
16
margin: auto;
17
display: block;
18
width: 100%;
19
max-width: 850px;
20
}
JavaScript
1
25
25
1
<div id="myModal" class="modal">
2
<span class="close">×</span>
3
<img class="modal-content" id="img" />
4
</div>
5
6
<div class="container-fluid padding gallerysection text-center" style="padding-bottom: 30px;">
7
<div class="row padding fadeineffect">
8
<div class="col-lg-6">
9
<img
10
class="modalImg"
11
src="https://lh3.googleusercontent.com/proxy/fl8D7wz4UGOL2OsOcYv_bLp6MBpsC5k_vCp1BhP-pUYcqXlUG1bJuCjRsWAWf2YLPt2pRCiUhd0tf3B4NismlNC5tBl6Ru1XOspcdp4U8ZGr9wCGYEri5ikCWn4YAJEmJNysu8KrlZVpdQ9sfg"
12
alt="MR Cello 1"
13
class="galleryphoto img-fluid"
14
/>
15
</div>
16
17
<div class="col-lg-6">
18
<img
19
class="modalImg"
20
src="https://lh3.googleusercontent.com/proxy/Y_PJq-iHuV0SwCw7t54ZZnk5WGXT6CLBsq2zsHb9VZnqx3R7M-Yk3_gVnwJa6cfns_x3HNvkuKV-q3e3VtTmQabjIDmazHwva2Bb2JIIxD4XOkRluLI-dm0cz1MDL_8Nd9tt7ACcxu_DAarebw"
21
alt="MR Cello 2"
22
/>
23
</div>
24
</div>
25
</div>