본문 바로가기
✨ Front-end/자바스크립트(JS)

[JavaScript] jQuery 이용해서 이미지 슬라이드 만들기 (캐러셀)

by 환풍 2023. 2. 19.
728x90
반응형

 

HTML

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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="main.css">
  <script src="result.js"> </script>
  
  <script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
</head>
<body>
  
 
  <!-- overflow : hidden 은 넘치는 부분 없애기다. -->
  <div style="overflow: hidden">
    <div class="slide-container">
      <div class="slide-box1">
        <p>1</p>
      </div>
      <div class="slide-box2">
        <p>2</p>
      </div>
      <div class="slide-box3">
       <p>3</p>
      </div>
    </div>
 </div> 
 
 <hr>
 
 <button class="slide-1">1</button>
  <button class="slide-2">2</button>
  <button class="slide-3">3</button>
 
<br><br>
 
<button class="next">다음</button>
 
 
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
 
 
cs

CSS

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
button{
  width:32%;
}
p{
  text-align: center;
  font-weight: bold;
  font-size: 40px;
}
.slide-container{
  width: 300vw;
  transition: all 1s;
}
.slide-box1{
  width: 100vw;
  float: left;
  background-color: aqua;
}
.slide-box2{
  width: 100vw;
  float: left;
  background-color:red;
}
.slide-box3{
  width: 100vw;
  float: left;
  background-color: green;
}
.next{
  width: 300px;
  margin: 0 auto;
  display: block;
}
cs

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
$('.slide-1').on('click'function(){
    $('.slide-container').css('transform','translateX(0vw)');
  })
  
  $('.slide-2').on('click'function(){
    $('.slide-container').css('transform','translateX(-100vw)');
  })
 
  $('.slide-3').on('click'function(){
    $('.slide-container').css('transform','translateX(-200vw)');
  })
 
 
     var slide = 1;
      $('.next').on('click',function(){
      if(slide % 3 ==1)
      {
        $('.slide-container').css('transform''translateX(-100vw)');
        slide++;
      }
      else if(slide % 3 ==2)
      {
        $('.slide-container').css('transform''translateX(-200vw)');
        slide++;
      }
      else if(slide % 3 == 0)
      {
        $('.slide-container').css('transform''translateX(0vw)');
        slide++;
      }
    });
cs

jQuery를 이용해서 if 대신 while을 넣어보았다. 그러자 갑자기 무한로딩에 걸려 어쩔수 없이 while을 쓰지 못해 코드가 굉장히 길어졌는데, 해결 방법에 대해 더 연구해보아야겠다.

 

See the Pen Untitled by park (@johong) on CodePen.

728x90
반응형

댓글