[英]javascript Carousel does not slide
I am designing a webpage with a fullscreen carousel. 我正在设计一个带有全屏轮播的网页。 The carousel does slide to the next images but however does not display the wanted images. 轮播会滑动到下一个图像,但是不会显示所需的图像。 I think the problem lies with my javascript work as I am not proficient in that language but it could also be html text I have included a link to my program on JSFiddle below 我认为问题出在我的javascript工作上,因为我不精通该语言,但也可能是html文本,我在下面的JSFiddle中包含了指向我的程序的链接
let sliderImages = document.querySelectorAll('.slide'); arrowLeft = document.querySelectorAll('#arrow-left'); arrowRight = document.querySelectorAll('#arrow-right'); current = 0; //clear all images function reset(){ for (let i = 0; i < sliderImages.length; i++){ sliderImages[i].style.display = 'none'; } } //start Slider (carousel) function startSlide(){ reset(); sliderImages[0].style.display = 'block'; } //show previous function slideLeft(){ reset(); sliderImages[current -1].style.display = 'block'; current--; } //show next function slideRight(){ reset(); sliderImages[current +1].style.display = 'block'; current++; } //Left arrow click arrowLeft.addEventlistener('click', function(){ if (current === 0){ current = sliderImages.length; } slideLeft(); }); //Right arrow click arrowRight.addEventListener('click', function(){ if (current === sliderImages.length - 1){ current = -1; } slideRight(); }); startSlide();
Looking at your code, you have 2 mistakes: one you have a typo in arrowLeft.addEventListener
, must be arrowLeft.addEventlistener
and second querySelectorAll
retrieve an array you have to select the first one, this would look like this: arrowLeft = document.querySelectorAll('#arrow-left')[0];
查看您的代码,您有2个错误:一个错误是arrowLeft.addEventListener
,必须是arrowLeft.addEventlistener
,第二个querySelectorAll
检索您必须选择第一个的数组,如下所示: arrowLeft = document.querySelectorAll('#arrow-left')[0];
. 。 Additional you bind onclick
and addEventListener
, only one is necessary. 另外,您需要绑定onclick
和addEventListener
,仅需要一个。
http://jsfiddle.net/xmkdwyer/2/ http://jsfiddle.net/xmkdwyer/2/
Your let sliderImages = document.querySelectorAll('slide')
isn't finding anything so your script is breaking. 您的let sliderImages = document.querySelectorAll('slide')
找不到任何内容,因此脚本被破坏了。 I changed that line to let sliderImages = document.getElementsByClassName('slide')
and it finds the divs successfully. 我将该行更改为let sliderImages = document.getElementsByClassName('slide')
,它成功找到了div。 I think you may need to tweak a few things around to get it working correctly, but the images do change. 我认为您可能需要进行一些调整才能使其正常工作,但是图像确实会发生变化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.