Calling an HTML file inside a Lightbox modal

Asked

Viewed 548 times

2

The case is as follows:

I use the Modal Lightbox for an image gallery, in the index I have the Thumbs and when clicking opens the modal with the image maximized.

Due to the amount of images it takes a long time to load all.

How can I call an html file containing the images into the modal of the index? So I would not load these images when loading the index.

function openModal() {
  document.getElementById('myModal').style.display = "block";
}

function closeModal() {
  document.getElementById('myModal').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  var captionText = document.getElementById("caption");
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " active";
  captionText.innerHTML = dots[slideIndex-1].alt;
}
body {
  font-family: Verdana, sans-serif;
  margin: 0;
}

* {
  box-sizing: border-box;
}

.row > .column {
  padding: 0 8px;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

.column {
  float: left;
  width: 25%;
}

/* The Modal (background) */
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: black;
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  width: 90%;
  max-width: 1200px;
}

/* The Close Button */
.close {
  color: white;
  position: absolute;
  top: 10px;
  right: 25px;
  font-size: 35px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #999;
  text-decoration: none;
  cursor: pointer;
}

.mySlides {
  display: none;
}

.cursor {
  cursor: pointer
}

/* Next & previous buttons */
.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -50px;
  color: white;
  font-weight: bold;
  font-size: 20px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
  -webkit-user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

img {
  margin-bottom: -4px;
}

.caption-container {
  text-align: center;
  background-color: black;
  padding: 2px 16px;
  color: white;
}

.demo {
  opacity: 0.6;
}

.active,
.demo:hover {
  opacity: 1;
}

img.hover-shadow {
  transition: 0.3s
}

.hover-shadow:hover {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">

<body>

<h2 style="text-align:center">Lightbox</h2>

<div class="row">
  <div class="column">
    <img src="http://www.lsphotografia.com/modal/img_nature.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://www.lsphotografia.com/modal/img_fjords.jpg" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://www.lsphotografia.com/modal/img_mountains.jpg" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://www.lsphotografia.com/modal/img_lights.jpg" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
  </div>
</div>

<div id="myModal" class="modal">
  <span class="close cursor" onclick="closeModal()">&times;</span>
  <div class="modal-content">

    <div class="mySlides">
      <div class="numbertext">1 / 4</div>
      <img src="http://www.lsphotografia.com/modal/img_nature_wide.jpg" style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">2 / 4</div>
      <img src="http://www.lsphotografia.com/modal/img_fjords_wide.jpg" style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">3 / 4</div>
      <img src="http://www.lsphotografia.com/modal/img_mountains_wide.jpg" style="width:100%">
    </div>
    
    <div class="mySlides">
      <div class="numbertext">4 / 4</div>
      <img src="http://www.lsphotografia.com/modal/img_lights_wide.jpg" style="width:100%">
    </div>
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    <div class="caption-container">
      <p id="caption"></p>
    </div>


    <div class="column">
      <img class="demo cursor" src="http://www.lsphotografia.com/modal/img_nature_wide.jpg" style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
    </div>
    <div class="column">
      <img class="demo cursor" src="http://www.lsphotografia.com/modal/img_fjords_wide.jpg" style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
    </div>
    <div class="column">
      <img class="demo cursor" src="http://www.lsphotografia.com/modal/img_mountains_wide.jpg" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
    </div>
    <div class="column">
      <img class="demo cursor" src="http://www.lsphotografia.com/modal/img_lights_wide.jpg" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
    </div>
  </div>
</div>


    
</body>
</html>

  • You can use a <iframe>, but in general I could solve this with a pre-loader and I wouldn’t need iframes, I just can’t provide an example, because your script doesn’t work and for this reason I don’t know where to start to modify it like this.

  • I edited the code and it’s working, I was without the full address of the images.

3 answers

0

Solved, I used the script passed by Guilherme Biancardi, instead of calling the html inside the modal I put a button calling the html with the complete modal, it was perfect.

If you want to check the address is http://gabifez12.lsphotografia.com

Thank you so much for your help.

0

Create a div with an id inside the modal and in javascript put:

$("#SEU_MODAL").click(function(){
    var request = $.ajax({
        url: "SUAPAGINA.html"
    });

    request.done(function (data) {
        $("#ID_DIV_MODAL").html(data);
    });

    request.fail(function () {
        console.log("Ocorreu um erro na requisição");
    });
});
  • Good morning Guilherme Biancardi, It did not work, the modal opens with nothing, does not load the html

  • Good morning, I did the test here and it worked perfectly, make sure the jQuery library is in the scope of your project, and make sure the id #SEU_MODAL is from the button that makes the modal open not the modal id.

  • Guilherme modal is opened in the onclick onclick="openModal();currentSlide(1)" function which references the myModal id within javascript. You could detail how you made it work by posting your complete script?

  • Good afternoon William, could you ask a question? I’m using the script the way I passed and I’m trying to do the following, after the function is called and load the external file, in case get.html and I needed when clicking another link the content that was loaded was dropped to load a new one. Is it possible to implement this in this script? Ex: Click to call get.html, the content is loaded then click to call pic.html the get.html is dropped and pic.html is loaded.

0


How you asked me for HTML:

<div id="abremodal" class="btn btn-primary " data-toggle="modal" data-target="#minhaModal">Open Modal</div>

<div class="modal fade" id="minhaModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <div id="conteudoModal">
                      nada
                    </div>
                </div>
            </div>
        </div>
    </div>

JS:

$(document).ready(function () {
    $("#abremodal").click(function(){
        var request = $.ajax({
            url: "endereco_da_sua_pagina_html.html"
        });

        request.done(function (data) {
            $("#conteudoModal").html(data);
        });

        request.fail(function () {
            console.log("Ocorreu um erro na requisição");
        });
    });
});
  • Guilherme, I really appreciate the help, but I couldn’t make it work, I added your script but it doesn’t load the html, it displays the images, but when you click it opens the modal with nothing.

  • Open the "F12" console and tell us which error appears. (Remember that my example I use bootstrap and jQuery as api, you need to adapt it to your modal and include jQuery in your HTML)

  • I already have Jquery included <script src="js/jquery.js"></script>, but I couldn’t adapt your script to html. Keeps opening the empty modal, the problem must be as mentioned, I could not adapt it correctly.

  • Guelherme, good afternoon I tried again here and unsuccessfully, if not ask too much I could try to adapt your script in the html I posted, it would help a lot.

Browser other questions tagged

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