Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lightbox gallery fullview仅适用于第一个选项卡(Javascript)_Javascript_Lightbox - Fatal编程技术网

Lightbox gallery fullview仅适用于第一个选项卡(Javascript)

Lightbox gallery fullview仅适用于第一个选项卡(Javascript),javascript,lightbox,Javascript,Lightbox,我正在一个网站上工作,我一直在按照教程建立一个灯箱画廊。不幸的是,它部分地起作用,因为我每年都创建选项卡,javascript只在第一个选项卡上执行,而不能在其他选项卡上执行。以下是我的lightbox js代码: const lightBoxContainer = document.querySelector(".lightbox"); const lightboxImage = document.querySelector(".lightbox-img"

我正在一个网站上工作,我一直在按照教程建立一个灯箱画廊。不幸的是,它部分地起作用,因为我每年都创建选项卡,javascript只在第一个选项卡上执行,而不能在其他选项卡上执行。以下是我的lightbox js代码:

const lightBoxContainer = document.querySelector(".lightbox");
const lightboxImage = document.querySelector(".lightbox-img");
const counter= document.querySelector(".lightbox-counter");
const prevButton= document.querySelector(".prev");
const nextButton= document.querySelector(".next");

// const lightboxText = document.querySelector(".lightbox-text");
const portfolioItems = document.querySelector(".portfolio-items").children;
let index;
let imgSrc;

// Close lightbox if click anywhere outside arrow
lightBoxContainer.addEventListener("click", function(event){
    if(event.target!==lightboxImage && event.target!==prevButton && event.target!==nextButton){
        lightBox();
    }
})

//on img click open img lightbox and changeimage information
for(let i=0; i<portfolioItems.length; i++){
    portfolioItems[i].querySelector(".fa").addEventListener("click",function(){
        index=i;
        changeImage();
        lightBox();
    })
}


// change img index +1 -1
function next(){
    if(index==portfolioItems.length-1){
        index=0
    }
    else{
        index++;
    }
    changeImage()
}

function prev(){
    if(index==0){
        index=portfolioItems.length-1;
    }
    else{
        index--;
    }
    changeImage()
}

// change img with arrow left/right
document.onkeydown = checkKey;

function checkKey(e) {

    e = e || window.event;

    if  (e.keyCode == '37') {
       prev()
    }
    else if (e.keyCode == '39') {
      next()
    }
    else if (e.keyCode == '27') {
        lightBox()
      }

}

// change css properties to open img
function lightBox(){
    lightBoxContainer.classList.toggle("open");
}

//make the image change according to index and change mentions according to img

function changeImage(){
    imgSrc=portfolioItems[index].querySelector("img").getAttribute("src");
    lightboxImage.src=imgSrc;
    counter.innerHTML=(index+1)+ " sur " + portfolioItems.length;
    // lightboxText.innerHTML=portfolioItems[index].querySelector("h2").innerHTML;
}
const lightBoxContainer=document.querySelector(“.lightbox”);
const lightboxImage=document.querySelector(“.lightbox img”);
常量计数器=document.querySelector(“.lightbox计数器”);
const prevButton=document.querySelector(“.prev”);
const nextButton=document.querySelector(“.next”);
//const lightbox text=document.querySelector(“.lightbox text”);
const portfolioItems=document.querySelector(“.portfolio items”).children;
let指数;
让imgSrc;
//如果单击箭头外的任意位置,请关闭灯箱
lightBoxContainer.addEventListener(“单击”),函数(事件){
if(event.target!==lightboxImage&&event.target!==prevButton&&event.target!==nextButton){
灯箱();
}
})
//在img上单击打开img灯箱并更改图像信息
for(设i=0;i