Javascript - Run multiple times

Asked

Viewed 88 times

0

Everybody, good afternoon, everybody

I have a JS + CSS code to open an image in normal size through a reduced image. I was able to do this with a single image, however when I put a looping in php, an error occurs.

Goes below:

<-- Aqui deveria começar o loop em php -->

<img id="myImg" src="imagem_grande.jpg">

<div id="myModal" class="modal">
<span class="close"><img src="imagem_close.png"></span>
<img src="imagem_grande.png" class="modal-content" width="100%">  <div id="caption"></div>
</div>

<-- Aqui deveria fechar o loop em php -->

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

// Get the image and insert it inside the modal - use its "alt" text as a caption
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;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>

I have no practice in JS.

I appreciate the help!

Abs!

1 answer

0

The probable errors are:

  • 1 . Define the same attribute of id for each image and modal.
  • 2 . Modifying only an image and a modal.
  • 3 . You are not going through all created images and modals.

To make your code work you will need to modify the HTML. Create a container and place the image and modal inside it. Add a class to the image of the varied execution, and also to some modal elements. To simplify:

<div class="row">
    <img class="my-img" src="imagem_grande.jpg"/>
    <div class="modal">
        <span class="close-btn">
            <img src="imagem_close.png"/>
        </span>
        <img src="imagem_grande.png" class="modal-content" width="100%">
        <div class="caption"></div>
    </div>
</div>

. Now, test this with Javascript:

var rows = document.getElementsByClassName("row");

/** ... Iterate each row with image and modal **/
for(var i = 0, row; row = rows[i]; i ++) {
    /* The big image */
    var myImg = row.getElementsByClassName("my-img")[0];
    /* Modal elements */
    var modal = row.getElementsByClassName("modal")[0];
    var modalCaptionText = modal.getElementsByClassName('caption')[0],
        modalCloseBtn = modal.getElementsByClassName('close-btn')[0],
        modalImg = modal.getElementsByTagName('img')[0];

    myImg.onclick = function() {
        modal.style.display = 'block';
        modal.src = this.src;
        modalImg.alt = this.alt;
        modalCaptionText.innerHTML = this.alt;
    };

    // When the user clicks on <span> (x), close the modal
    modalCloseBtn.onclick = function() {
        modal.style.display = "none";
    };
}
  • Did not run, my dear. I posted the JS as you mentioned above, without modifications. Re-established the php code, now with more details, including the while loop:

  • while($file = readdir($diretorio)) { $analyzes = explode(".",$file); if ($analyzes[1] == "jpeg" or $analyzes[1] == "jpg") ' if($checker==0) echo "<tr>"; ? > <th align="left"> <div class="Row"> <img class="my-img" src="<? php echo $path. $file; ? >"/> <div class="modal"><span class="close-btn"> <img src="images/facebook_yellow.png"/> </span> <img src="<? php echo "photos/". $categoria." /". $file; ? >" class="modal-content"><div class="caption"></div> </div> </div> </th> <? php if($checker==3) { echo "</tr>"; $checker=-1 ;} $checker++ } }

  • @Felipe Atualizei. I missed the string of a css class in js (from "close" to "close-btn": var modalCloseBtn = modal.getElementsByClassName('close-btn')[0];). Your PHP is correct.

  • If I tell you it hasn’t worked out yet, you believe me? =(

  • @Felipe Well, have you seen the console? No problem. This is very easy to fix! PS: at least here worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.