This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMG_Carousel_nayoung.js
51 lines (42 loc) · 1.69 KB
/
IMG_Carousel_nayoung.js
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
function carouselSlideLeft(){
// map으로 콜백함수 실행해서 index값 옯기자.
// 0번->1번, 1번 -> 2번, 2번->0번
// index인.. 0하고 1을 i값을 +1 2는 -1 > if
imgArr.unshift(imgArr[2]);
imgArr.pop(imgArr[2]);
//우측방향은
// imgArr.push(imgArr[2]);
// imgArr.shift(imgArr[2]);
imgArr.map(function(element,index,array){
element.setAttribute('src',imgSrcArr[index]);
// console.log(imgArr);
});
// 이미지 src를 다음 img태그, 다음인덱스 값으로 옮기기
// img id는 유지. src만 옮겨지는 것.
};
function carouselSlideRight(){
imgArr.push(imgArr[0]);
imgArr.shift(imgArr[0]);
imgArr.map(function(element,index,array){
element.setAttribute('src',imgSrcArr[index]);
});
};
// const timeoutID;
let timeout;
// const stoptime = clearInterval(timeout);
const img1= document.getElementById('img-1');
const img2 = document.getElementById('img-2');
const img3 = document.getElementById('img-3');
const img1Src = img1.getAttribute('src');
const img2Src = img2.getAttribute('src');
const img3Src = img3.getAttribute('src');
const imgArr = [img1, img2, img3];
const imgSrcArr = [img1Src, img2Src, img3Src];
const imgLeftSlide = document.querySelector('#imgLeftBtn');
const imgRightSlide = document.querySelector('#imgRightBtn');
const imgAutoSlide = document.querySelector('#autoSlideIcon');
const stopSlide = document.querySelector('#stopIcon');
imgLeftSlide.addEventListener('click', carouselSlideLeft);
imgRightSlide.addEventListener('click', carouselSlideRight);
imgAutoSlide.addEventListener('click', function(){timeout = setInterval(carouselSlideLeft,3000)});
stopSlide.addEventListener('click', function(){clearInterval(timeout)});