본문 바로가기

프로그래밍 공부/jQuery

jQuery - 이미지 슬라이더

<!DOCTYPE html>
<html>
<head>
     <title>Document</title>
     <style>   
          *margin0pxpadding0px; }

          /* Animation Canvas */
          .animation-canvas {
               positionrelative;
               overflowhidden;
               width600pxheight400px;
          }

          /* Slider Panel */
          .slider-panel { width3000pxpositionrelative; }
          .slider-image { floatleftwidth600pxheight400px; }

          /* Slider Text Panel */
          .slider-text-panel { positionabsolutetop200left50px; }
          .slider-text { positionabsolutewidth250pxheight150px; }

          /* Control Panel */
          .control-panel {
               positionabsolutetop380px;
               left270height13px;
               overflowhidden;
          }
          .control-button {
               width12pxheight46px;
               positionrelative;
               float:leftcursorpointer;
               backgroundurl('point_button.png');
          }
          .control-button:hover { top-16px; }
          .control-button:active { top-31px; }

          /* 폰트 변경 */
          div>h1 { 
               colorred
               text-shadow2px 2px 2px black
          }
          p {
               colorred;
               text-shadow2px 2px 2px black;
          }
     </style>
     <script src="jquery-3.4.1.js"></script>
     <script>
          $(document).ready(function(){
          // 슬라이더를 움직여주는 함수
               function moveSlider(index){
                    // 슬라이더를 이동한다.
                    let willMoveLeft = -(index * 600);
                    $('.slider-panel').animate({ left: willMoveLeft }, 'slow');


                    // control-button에 active 클래스를 부여/제거한다.
                    $('.control-button[data-index=' + index + ']').addClass('active');
                    $('.control-button[data-index!=' + index + ']').removeClass('active');


                    // 글자를 이동한다.
                    $('.slider-text[data-index=' + index + ']').show().animate({
                         left: 0
                    }, 'slow');

                    $('.slider-text[data-index!=' + index + ']').hide('slow'function(){
                          $(this).css('left', -300);
                    });
               }

               // 초기 텍스트 위치 지정 및 data-index 할당
               $('.slider-text').css('left', -300).each(function(index){
                    $(this).attr('data-index'index);
               });

               // 컨트롤 버튼의 클릭 리스너 지정 및 data-index 할당
               $('.control-button').each(function(index){
                    $(this).attr('data-index'index);
               }).click(function(){
                    let index = $(this).attr('data-index');
                    moveSlider(index);
               });

               // 초기 슬라이더 위치 지정
               let radomNumber = Math.round(Math.random()*4);
               moveSlider(radomNumber);
          });
     </script>
</head>
<body>
     <h1>World War 2 : Pacific War 1941 – 1945</h1>
     <div class="animation-canvas">
          <div class="slider-panel">
               <img class="slider-image" src=".\Image\The Attack on Pearl Harbor.jpg">
               <img class="slider-image" src=".\Image\The Battle of Midway.jpg">
               <img class="slider-image" src=".\Image\Battle of Guadalcanal.jpg">
               <img class="slider-image" src=".\Image\Battle of the Philippine Sea.jpg">
               <img class="slider-image" src=".\Image\The Battle of Leyte Gulf.jpg">
          </div>
          <div class="slider-text-panel">
               <div class="slider-text">
                    <h1>The Attack on Pearl Harbor</h1>
                    <p>December 7, 1941</p>
               </div>
               <div class="slider-text">
                    <h1>The Battle of Midway</h1>
                    <p>4–7 June 1942</p>
               </div>
               <div class="slider-text">
                    <h1>Battle of Guadalcanal</h1>
                    <p>7 August 1942 – 9 February 1943</p>
               </div>
               <div class="slider-text">
                    <h1>Battle of the Philippine Sea</h1>
                    <p>June 19–20, 1944</p>
               </div>
               <div class="slider-text">
                    <h1>The Battle of Leyte Gulf</h1>
                    <p>17 October – 26 December 1944</p>
               </div>
          </div>
          <div class="control-panel">
               <div class="control-button"></div>
               <div class="control-button"></div>
               <div class="control-button"></div>
               <div class="control-button"></div>
               <div class="control-button"></div>
          </div>
     </div>
     <h1>United States of America VS Empire of Japan</h1>
</body>
</html>

이미지 슬라이더.zip
0.35MB

HTML 태그 구성

slider-panel은 이미지를 담는 패널로, 움직이며 이미지가 전환되는 효과를 만든다.
slider-text-panel은 글자를 답는 패널이다.
control-panel은 컨트롤 버튼을 담는 패널이다.

control-button은 이미지지만 img 태그를 사용하지 않는다, 이유는 <div>태그로 만들고 스타일시트에서 background 속성으로 이미지를 설정하는 식으로 사용을 한다. 이렇게 되면 스타일시트의 hover 필터와 active 필터를 사용할 수 있기 때문이다.

Style 태그 구상

control-button 클래스의 hover와 active에 top 속성을 준 이유는 이미지파일 point_button.png 파일은 세로로 늘어진 이미지이다. 이 부분에 표시 부분을 제한하고 위 아래로 옮김으로써 이미지가 변하는 것과 비슷한 효과를 주는 것이다.

자바스크립트 구성

자바스크립트의 구성 순서는 아래와 같다.

1. 초기 텍스트 위치를 지정한다.
2. 이벤트를 연결한다.
3. 각각의 텍스트와 컨트롤 버튼의 위치를 알 수 있게 data-index를 할당한다.
4. 이동하는 함수를 만든다.
5. 초기 슬라이더 위치를 설정한다.

data-index는 다른 이름으로 바꿔도 문제가 없다. 

'프로그래밍 공부 > jQuery' 카테고리의 다른 글

jQuery - 캔버스 애니메이션을 위한 틀  (0) 2019.12.30
jQuery - 효과  (0) 2019.12.30
jQuery - 이벤트 下  (0) 2019.12.27
jQuery - 이벤트 上  (0) 2019.12.27
jQuery - 문서 객체 조작  (0) 2019.12.24