function simpleSlider(element) {
const root = typeof element === 'string'
? document.querySelector(element)
: element;
const items = root.querySelectorAll('.slider__item');
const prev = root.querySelector('.slider__btn-prev');
const next = root.querySelector('.slider__btn-next');
let index = 0;
function slideTo(newIndex) {
items[index].classList.remove('slider__item--active');
index = (newIndex + items.length) % items.length;
items[index].classList.add('slider__item--active');
}
prev.addEventListener('click', () => slideTo(index - 1));
next.addEventListener('click', () => slideTo(index + 1));
}
simpleSlider('#slider1');
simpleSlider(document.querySelector('#slider2'));