oddhtml / script.js
chohj06ms's picture
Update script.js
39a8714 verified
raw
history blame
2.21 kB
//์Šฌ๋ผ์ด๋” ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
let currentIndex = 0;
//๋ฐ‘ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜๋“ค ์„ ์–ธ(์ดˆ๊ธฐํ™”)
let startTouchPosition = 0;
let endTouchPosition = 0;
const slider = document.querySelector('.slider');
const totalSlides = slider.children.length;
//์Šฌ๋ผ์ด๋“œ๋ฅผ ์˜ฎ๊ธฐ๋Š” ํ•จ์ˆ˜
function moveToSlide(n) {
let newSlidePosition = ((n - 1) * -100) / totalSlides;
slider.style.transform = `translateX(${newSlidePosition}%)`;
currentIndex = n;
}
// ํŽ˜์ด์ง€ ๋กœ๋“œ ์‹œ ๋‘ ๋ฒˆ์งธ ์Šฌ๋ผ์ด๋“œ๋กœ ์ด๋™
document.addEventListener('DOMContentLoaded', () => {
moveToSlide(currentIndex); // ์ดˆ๊ธฐ ์œ„์น˜ ์„ค์ •
});
// ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
//๊ฐ€์žฅ ์™ผ์ชฝ์— ์œ„์น˜ํ•˜๋ฉด ๋งˆ์ด๋„ˆ์Šค ๋˜๋Š”๊ฑธ ๋ง‰๊ธฐ์œ„ํ•œ if๋ฌธ
if (currentIndex > 0) {
//ํ˜„์žฌ ์œ„์น˜ ๋นผ๊ธฐ 1
moveToSlide(currentIndex - 1);
}
} else if (e.key === 'ArrowRight') {
//๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•˜๋ฉด ํ”Œ๋Ÿฌ์Šค ๋˜๋Š”๊ฑธ ๋ง‰๊ธฐ์œ„ํ•œ if๋ฌธ
if (currentIndex < totalSlides - 1) {
//ํ˜„์žฌ ์œ„์น˜ ๋”ํ•˜๊ธฐ 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 > 0) {
// ์™ผ์ชฝ์œผ๋กœ ์Šฌ๋ผ์ด๋“œ
moveToSlide(currentIndex - 1);
}
}
// ๋งˆ์šฐ์Šค ํœ  ์ด๋ฒคํŠธ
slider.addEventListener('wheel', (e) => {
e.preventDefault(); // ํŽ˜์ด์ง€ ์Šคํฌ๋กค ๋ฐฉ์ง€
if (e.deltaY < 0 && currentIndex > 0) {
// ํœ ์„ ์œ„๋กœ ์Šคํฌ๋กค (์ด์ „ ์Šฌ๋ผ์ด๋“œ๋กœ ์ด๋™)
moveToSlide(currentIndex - 1);
} else if (e.deltaY > 0 && currentIndex < totalSlides - 1) {
// ํœ ์„ ์•„๋ž˜๋กœ ์Šคํฌ๋กค (๋‹ค์Œ ์Šฌ๋ผ์ด๋“œ๋กœ ์ด๋™)
moveToSlide(currentIndex + 1);
}
});