16 lines
566 B
JavaScript
16 lines
566 B
JavaScript
|
function slide(containerId, direction) {
|
||
|
const container = document.getElementById(containerId);
|
||
|
const slides = container.querySelectorAll('.slide');
|
||
|
const currentScroll = container.scrollLeft;
|
||
|
const slideWidth = slides[0].offsetWidth;
|
||
|
|
||
|
const currentIndex = Math.round(currentScroll / slideWidth);
|
||
|
const newIndex = direction === 'next' ?
|
||
|
Math.min(currentIndex + 1, slides.length - 1) :
|
||
|
Math.max(currentIndex - 1, 0);
|
||
|
|
||
|
container.scrollTo({
|
||
|
left: newIndex * slideWidth,
|
||
|
behavior: 'smooth'
|
||
|
});
|
||
|
}
|