简体   繁体   English

Vanilla JS图像滑块使用数组保存不起作用的图像

[英]Vanilla JS image slider using array to hold images not working

I'm trying to create a slider that will pull imgs from an array and then display them on a page so that more can be added later if needed. 我正在尝试创建一个滑块,该滑块将从数组中提取img,然后将其显示在页面上,以便以后可以根据需要添加更多。 The problem I have is the last img in the array is showing but I cannot get the imgs to cycle thru with the click events I've created. 我遇到的问题是显示了数组中的最后一个img,但是我无法使img通过我创建的click事件循环。

I've tried using a few different tutorials I've found online but most of them use an automatic slide function. 我尝试使用网上找到的一些不同的教程,但是其中大多数使用自动幻灯片功能。

let current = 0;
let slider = document.getElementById('slide-show')
let slides = slider.childNodes;
let arrowLeft = document.getElementById('arrow-left');
let arrowRight = document.getElementById('arrow-right');
let imgs = [
  '../img/img1.jpeg',
  '../img/img2.jpeg',
  '../img/img3.jpeg',
  '../img/img4.jpeg',
  '../img/img5.jpg'
];

function slideShow() {
  for(let i = 0; i < imgs.length; i++){
    let img = document.createElement('img');
    img.src = imgs[i];
    slider.appendChild(img)
  }
}

arrowLeft.addEventListener('click', function slideLeft(){
  if(current === 0) {
    current = slides.length;
    current--;
  }
});

arrowRight.addEventListener('click', function slideRight() {
  if(current === slides.length) {
    current = 0;
    current++;
  }
})

slideShow();
CSS

#slide-show {
  position: relative;
}

 #slide-show img {
  height: 300px;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

expected that function SlideLeft() will naturally allow you to move backwards in the img list. 期望函数SlideLeft()自然会允许您在img列表中向后移动。 and of course arrowRight() will move it forward. 当然arrowRight()会将其向前移动。

https://codepen.io/levi_blodgett/pen/dEayLY https://codepen.io/levi_blodgett/pen/dEayLY

It seems to me like your DOM manipulation was a bit off from what I could tell, in this case I would say it is most effective to just check as well on event click rather than a for loop with each image constantly there. 在我看来,您对DOM的操作与我所能说的有些出入,在这种情况下,我想说最有效的方法是仅检查事件单击,而不是始终在每个图像上进行for循环。

In this case what we do it change the image instead of how you had it before which was have each image layered behind another, which may have been unintentional. 在这种情况下,我们要做的是更改图像,而不是像以前那样改变每个图像的层次,这可能是无意的。

So instead of: 所以代替:

function slideShow() {
for(let i = 0; i < imgs.length; i++){
    let img = document.createElement('img');
    img.src = imgs[i];
    slider.appendChild(img)
  }
}

We have: 我们有:

function slideShow(imgCount) {
    let currentImg = document.getElementById('img');
    currentImg.src = imgs[imgCount];
}

Which should be more clear what you are doing, setting the image to the current iteration that you have your event listeners iterate for you. 应该更清楚地知道正在做什么,将图像设置为让事件侦听器为您迭代的当前迭代。 The image is set into the HTML so it can edit the HTML instead of having to create an element on it's own, but you could do that if you wished, you'd just have to append after the left arrow probably which would be more difficult to follow and execute. 该图像已设置为HTML,因此它可以编辑HTML,而不必自己创建元素,但是如果您愿意,可以这样做,您只需要在向左箭头后追加,可能会更困难遵循并执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM