let currentIndex = 1; // 인덱스 1에서 시작하도록 수정 const slider = document.querySelector('.slider'); const totalSlides = slider.children.length; function moveToSlide(n) { let newSlidePosition = ((n - 1) * -100); // 첫 번째 슬라이드를 중앙에 위치시킴 slider.style.transform = `translateX(${newSlidePosition}vw)`; } // 초기 슬라이드 위치 설정 moveToSlide(currentIndex); // 키보드 이벤트 핸들러 document.addEventListener('keydown', (e) => { if (e.key === 'ArrowLeft') { if (currentIndex > 1) { // 0 대신 1을 사용 moveToSlide(currentIndex - 1); } } else if (e.key === 'ArrowRight') { if (currentIndex < totalSlides - 1) { moveToSlide(currentIndex + 1); } } }); // 터치 이벤트 시작 slider.addEventListener('touchstart', (e) => { startTouchPosition = e.touches[0].clientX; }); // 터치 이벤트 끝 slider.addEventListener('touchend', (e) => { endTouchPosition = e.changedTouches[0].clientX; handleTouchMove(); }); function handleTouchMove() { if (startTouchPosition - endTouchPosition > 50 && currentIndex < totalSlides - 1) { moveToSlide(currentIndex + 1); } else if (endTouchPosition - startTouchPosition > 50 && currentIndex > 1) { // 0 대신 1을 사용 moveToSlide(currentIndex - 1); } }