File size: 1,417 Bytes
757a69e
2da0183
 
b35555d
 
 
 
3e8a03d
 
 
 
 
757a69e
 
 
 
 
 
2da0183
b35555d
 
2da0183
b35555d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2da0183
b35555d
2da0183
 
b35555d
 
 
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
let currentIndex = 1;
let startTouchPosition = 0;
let endTouchPosition = 0;

const slider = document.querySelector('.slider');
const totalSlides = slider.children.length;

// ํŽ˜์ด์ง€ ๋กœ๋“œ ์‹œ ๋‘ ๋ฒˆ์งธ ์Šฌ๋ผ์ด๋“œ๋กœ ์ด๋™
document.addEventListener('DOMContentLoaded', () => {
  moveToSlide(currentIndex); // ์ดˆ๊ธฐ ์œ„์น˜ ์„ค์ •
});

function moveToSlide(n) {
  let newSlidePosition = (n * -100) / totalSlides;
  slider.style.transform = `translateX(${newSlidePosition}%)`;
  currentIndex = n;
}

// ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ
document.addEventListener('keydown', (e) => {
  if (e.key === 'ArrowLeft') {
    if (currentIndex > 0) {
      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 > 0) {
    // ์™ผ์ชฝ์œผ๋กœ ์Šฌ๋ผ์ด๋“œ
    moveToSlide(currentIndex - 1);
  }
}