Error
Uncaught TypeError: this.slides.forEach is not a function
at Slider.showSlides (slider.js:18)
at Slider.plusSlides (slider.js:26)
at HTMLAnchorElement. (slider.js:32)
main.js
import Slider from "./modules/slider";
window.addEventListener('DOMContentLoaded', () => {
const slider = new Slider('.page', '.next');
slider.render();
});
slider.js
export default class Slider {
constructor(page, btns) {
this.page = document.querySelector(page);
this.slides = this.page.children;
this.btns = document.querySelectorAll(btns);
this.slideIndex = 1;
}
showSlides(n) {
if (n > this.slides.length) {
this.slideIndex = 1;
}
if (n < 1) {
this.slideIndex = this.slides.length;
}
this.slides.forEach(slide => {
slide.style.display = 'none';
});
this.slides[this.slideIndex - 1].style.display = 'block';
}
plusSlides(n) {
this.showSlides(this.slideIndex += n);
}
render() {
this.btns.forEach(item => {
item.addEventListener('click', () => {
this.plusSlides(1);
});
});
this.showSlides(this.slideIndex);
}
}