0

Hello i got a problem with script execution. I'm using JQuarry for image loading in load_images() and then executing activate_images() function, which sets "data-lightbox" attribute and class to "active". Problem is when I first load a page, the activate_images() is somehow executed before images are in .document so it has no effect. I have tried lot of ways how to postpone the execution after page load but no success. Has someone have an idea how to solve this problem?

index.html

 <html>
  <head>
    <link rel="stylesheet" href="styles/lightbox.min.css">
    <script type="text/javascript" src="lightbox-plus-jquery.min.js"></script>
  </head>
  <body>
   <section class="gallery" id="galerie" style="padding-bottom: 300px;">
      <h2>galerie</h2>
      <div class="button-container">
        <button id="vse" onclick="reload_images(this.id)">vše</button>
        <button id="kuchyne" onclick="reload_images(this.id)">kuchyně</button>
        <button id="vestavne-skrine" onclick="reload_images(this.id)">vestavné skříně</button>
        <button id="loznice" onclick="reload_images(this.id)">ložnice</button>
        <button id="doplnky" onclick="reload_images(this.id)">doplňky</button>
        <button id="detske-pokoje" onclick="reload_images(this.id)">dětské pokoje</button>
        <button id="prace-z-masivu" onclick="reload_images(this.id)">práce z masivu</button>
      </div>
      <div class="gallery-container">

      </div>
      
    </section>
     <script src="gallery.js"></script>
     <script>
      window.onload = function() {
        load_all();
      }
      
     </script>
  </body>

gallery.js

  {id:"detske-pokoje", loaded:false},
  {id:"doplnky", loaded:false},
  {id:"kuchyne", loaded:false},
  {id:"loznice", loaded:false},
  {id:"prace-z-masivu", loaded:false},
  {id:"vestavne-skrine", loaded:false},
];


function reload_images(id){

  turn_off_images();

  if(id === "vse"){
    load_all();
    return;
  }

  for (let type of types){
    if(type.id === id){
      if(!type.loaded){
        load_images(id);
        type.loaded = true;
      }
      activate_images(id);
    }
  }
}

function load_all(){

  for (let type of types){
    if(!type.loaded){
      load_images(type.id);
      type.loaded = true;
    }
    activate_images(type.id);
  }
}

function load_images(id){
  let source = "gallery/";
  let folder = source + id;

  $.ajax({
      url : folder,
      success: function (data) {
          $(data).find("a").attr("href", function (i, val) {
              if( val.match(/\.(jpe?g|png|gif)$/) ) { 
                  $(".gallery-container").append( "<a href='" + val +"' id='" + id + "'><img src='" + val +"'></a>" );
              } 
          });
      }
  });
}

function turn_off_images(){
  let links = document.querySelectorAll(".gallery-container a");
  for(let i = 0; i < links.length; i++){
    links[i].className = "non-active";
    links[i].removeAttribute("data-lightbox");
  }
}

function activate_images(id){
  let links = document.querySelectorAll(".gallery-container a");
  for(let i = 0; i < links.length; i++){
    if(links[i].id === id){
      links[i].className = "active";
      links[i].setAttribute("data-lightbox","mygallery");
    }     
  }
}

1
  • from what i can see you need to call activate_images from your success function in the ajax call
    – matt
    Commented Feb 27 at 21:34

1 Answer 1

0

I find that simply adding the "data-lightbox" attribute and class "active" directly in function load_images() fixed the problem.

function load_images(id){
  let source = "gallery/";
  let folder = source + id;

  $.ajax({
      url : folder,
      success: function (data) {
          $(data).find("a").attr("href", function (i, val) {
              if( val.match(/\.(jpe?g|png|gif)$/) ) { 
                  $(".gallery-container").append( "<a class='"+ "active" + "'data-lightbox='"+ "mygallery" +"' href='" + val +"' id='" + id + "'><img src='" + val +"'></a>" );
              } 
          });
      }
  });
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.