oddhtml / script.js
chohj06ms's picture
Update script.js
2fece8f verified
raw
history blame
No virus
1.32 kB
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);
}
}