Asthard

Untitled

Apr 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var scrollPosition = "";
  2. var body = document.querySelector("body");
  3. function autoLightBox(objeto){
  4.  
  5.     var lightboxes = document.querySelectorAll(objeto);
  6.  
  7.  
  8.     lightboxes.forEach(function(item, index){
  9.  
  10.         let containerID = 'lightbox' + (index+1);
  11.         item.setAttribute("id", containerID);
  12.  
  13.         let template = "";
  14.  
  15.         let newHTML = "";
  16.  
  17.         let imgs = item.querySelectorAll("img");
  18.  
  19.  
  20.         for (i=0; i< imgs.length; i++){
  21.  
  22.             let imgID = 'lightbox' + (index+1) + '_img'+(i+1);
  23.             let imgURL = imgs[i].getAttribute("src");
  24.             let imgAltOriginal= imgs[i].getAttribute("alt");
  25.             let imgTitle = "";
  26.             let imgDescription = "";
  27.             let imgPrev = "";
  28.             let imgNext = "";
  29.            
  30.            
  31.             if(imgAltOriginal){
  32.                 imgTitle = imgAltOriginal.split(": ")[0];
  33.                 imgDescription = imgAltOriginal.split(": ")[1];
  34.             }
  35.             if(i > 0 || i < (imgs.length - 1))
  36.             {
  37.                 imgPrev = 'lightbox' + (index + 1) + '_img' + i;
  38.                 imgNext = 'lightbox' + (index + 1) + '_img' + (i + 2);
  39.                 //console.log("i:" + i + " | "  + "imgPrev:" + imgPrev + " | " + "imgNext: " + imgNext);
  40.             }
  41.             if(i == 0)
  42.             {
  43.                 imgPrev = 'lightbox' + (index + 1) + '_img' + (i + imgs.length);
  44.                 //console.log("i:" + i + " | " + "imgPrev:" + imgPrev);
  45.             }
  46.             if(i == (imgs.length - 1))
  47.             {
  48.                 imgNext = 'lightbox' + (index + 1) + "_img" + (i - imgs.length + 2);
  49.                 //console.log("i:" + i + " | " + "imgNext: " + imgNext);
  50.             }
  51.  
  52.            
  53.             template = '<li class="item"><a href="#' + imgID + '" class="thumbnail" style="background-image: url(' +  imgURL +  ')"><span class="easing">' + imgTitle + '</span></a><div class="large" id="' + imgID + '"><a href="#' + 'lightbox' + (index + 1) + '" class="overlay"></a><div class="img-area easing"><div class="img-box"><img src="' + imgURL + '" alt="' + imgAltOriginal + '"></div><h3>' + imgTitle + '</h3><p>' + imgDescription + '</p></div></div></li>'
  54.  
  55.             newHTML += template;
  56.             lightboxes[index].innerHTML = '<ul class="list">' + newHTML + '</ul>';
  57.  
  58.         };
  59.  
  60.  
  61.         var links = document.querySelectorAll(".thumbnail");
  62.  
  63.         links.forEach(function(link, count){
  64.  
  65.             link.addEventListener("click", function(event){
  66.                 scrollPosition = window.pageYOffset;
  67.             });
  68.         });
  69.     });
  70. };
  71.  
  72. autoLightBox(".auto-lightbox");
  73.  
  74.  
  75. window.onhashchange = function(){
  76.     let urlImagem = location.hash;
  77.     if (urlImagem.indexOf("_") != -1){
  78.         body.style.overflow = "hidden";
  79.     }else{
  80.         body.style.overflow = "visible";
  81.     }
  82.     window.scrollTo(0, scrollPosition);
  83. };
Advertisement
Add Comment
Please, Sign In to add comment