Home Creating a modal for multiple images
Reply: 1

Creating a modal for multiple images

user8946239
1#
user8946239 Published in 2017-12-07 22:59:17Z

Hello I'm trying to find a solution to having a modal appear when I click on an image on a carousel. I have done this for one of my images however I want to use this same bit of code for around 20 images. Is there a way to do this without copying the code for each image? This is using bootstrap but I dont think it is needed to be able to do this, thanks.

    <html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <title>Photography</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/styles.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

    <style>


    </style>

<body>

    <div class="container" style="padding-bottom:15px;">
         <div class="row"> 
             <div class="col-2"></div>
             <div class="col-8">
                 <h2>Carousel Example</h2> 
                  <div id="myCarousel" class="carousel slide" data-ride="carousel">
                    <!-- Indicators -->
                    <ol class="carousel-indicators">
                      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                      <li data-target="#myCarousel" data-slide-to="1"></li>
                      <li data-target="#myCarousel" data-slide-to="2"></li>
                    </ol>

                    <!-- Wrapper for slides -->
                    <div class="carousel-inner">
                      <div class="item active">
                        <img src="Photos/Buttons/BridgeNON.png" alt= "Bridge" class="img‐fluid" onmouseover="this.src='Photos/Buttons/Bridge.png'" onmouseout="this.src='Photos/Buttons/BridgeNON.png'" style="width:100%;">
                      </div>

                      <div class="item">
                        <img id="myImg" src="Photos/Buttons/waterNON.png" alt= "Water" class="img‐fluid" onmouseover="this.src='Photos/Buttons/water.png'" onmouseout="this.src='Photos/Buttons/waterNON.png'" style="width:100%;">
                      </div>

                      <div class="item">
                        <img src="" alt="" style="width:100%;">
                      </div>
                    </div>


                    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                      <span class="glyphicon glyphicon-chevron-left"></span>
                      <span class="sr-only">Previous</span>
                    </a>
                    <a class="right carousel-control" href="#myCarousel" data-slide="next">
                      <span class="glyphicon glyphicon-chevron-right"></span>
                      <span class="sr-only">Next</span>
                    </a>
                  </div>
             </div>
             <div class="col-2"></div>
        </div>
    </div>

    <div id="myModal" class="modal">
        <span class="close">&times;</span>
        <img class="modal-content" id="img01">
        <div id="caption"></div>
    </div>

    <script>
var modal = document.getElementById('myModal');

var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

var span = document.getElementsByClassName("close")[0];

span.onclick = function() { 
    modal.style.display = "none";
}
</script>

</body>
</html>
Chris Happy
2#
Chris Happy Reply to 2017-12-07 23:03:21Z

Replace your Javascript with the following. This gets all the images in #myCarousel, loops through them, and adds the event.

var modal = document.getElementById('myModal');

var imgs = document.getElementById('myCarousel').getElementsByTagName('img');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");

for (var i = 0; i < imgs.length; i++) {
  imgs[i].onclick = function() {
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
  }
}

var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
  modal.style.display = "none";
}

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.343758 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO