File size: 2,099 Bytes
1542a7f ea8b029 1542a7f 2da0183 b35555d 1542a7f 757a69e 3457014 757a69e 62c2791 2da0183 b35555d adb5801 2da0183 b35555d adb5801 b35555d adb5801 b35555d 2da0183 b35555d 2da0183 b35555d 39a8714 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
//์ฌ๋ผ์ด๋ ์ด๊ธฐ๊ฐ ์ค์
let currentIndex = 1;
//๋ฐ ์ฝ๋์์ ์ฌ์ฉํ ๋ณ์๋ค ์ ์ธ(์ด๊ธฐํ)
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' || e.key === 'a') {
// 'ArrowLeft', 'a', 'ใ
' ํค ์ฒ๋ฆฌ
if (currentIndex > 0) {
moveToSlide(currentIndex - 1);
}
} else if (e.key === 'ArrowRight' || e.key === 'd') {
// 'ArrowRight', 'd' ํค ์ฒ๋ฆฌ
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 > 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);
}
});
|