asimryu

jquery slide 20141030-4 최우연 코드

Oct 30th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.53 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>slide</title>
  6. <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
  7. <style>
  8.     /* 태그 */
  9.     body { margin:0; background:#333; }
  10.     button { padding:3px 10px; background:#bebebe; border:none; background:#33a0ff; color:#fff; font-weight:bold; font-family:"Malgun Gothic"; cursor:pointer; }
  11.     button:hover { opacity:.7; }
  12.    
  13.     /* 아이디 */
  14.     #slidebox { position:relative; width:500px; height:300px; margin:0 auto; overflow:hidden; margin-top:50px; }
  15.     #slide { width:500px; height:300px; list-style:none; padding:0; margin:0; }
  16.     #slide li { position:absolute; left:0; top:0; }
  17.     #numbers { margin-bottom:10px; }
  18.    
  19.     /* 클래스 */
  20.     .title { width:100%; position:absolute; bottom:0; padding:20px 0; background:rgba(0,0,0,0.5); font-family:"Malgun Gothic"; color:#fff; font-weight:bold; text-align:center; font-size:18px; }
  21.     .center { position:relative; margin:20px auto; width:210px; }
  22.     .mt20 { margin-top:20px; }
  23.     .p { background:#bebebe; width:10px; height:10px; border-radius:50%; float:left; margin-right:20px; cursor:pointer; position:relative; z-index:10; }
  24.     .p:last-child { margin-right:0; }
  25.     .wh { width:100%; float:left; margin-bottom:20px; }
  26.     .on { background:#33a0ff; cursor:pointer; }
  27.    
  28. </style>
  29. </head>
  30.  
  31. <body>
  32.     <!-- 코드 by 최우연 -->
  33.     <!-- 슬라이드 -->
  34.     <div id="slidebox">
  35.         <ul id="slide">
  36.             <li title="음식"><img src="http://lorempixel.com/500/300/food"></li>
  37.             <li title="자연"><img src="http://lorempixel.com/500/300/nature"></li>
  38.             <li title="스포츠"><img src="http://lorempixel.com/500/300/sports"></li>
  39.             <li title="도시"><img src="http://lorempixel.com/500/300/city"></li>
  40.             <li title="사람"><img src="http://lorempixel.com/500/300/people"></li>
  41.             <li title="비즈니스"><img src="http://lorempixel.com/500/300/business"></li>
  42.             <li title="패션"><img src="http://lorempixel.com/500/300/fashion"></li>
  43.         </ul>
  44.     </div>
  45.    
  46.     <!-- 위치버튼 -->
  47.     <div id="numbers" class="center">
  48.         <div class="wh"></div>
  49.     </div>
  50.    
  51.     <!-- 버튼 -->
  52.     <div class="center mt20">
  53.         <button id="prev" onClick="animation('prev')">이전</button>
  54.         <button id="next" onClick="animation('next');">다음</button>
  55.         <button id="up" onClick="animation('up');">&nbsp;&nbsp;&nbsp;</button>
  56.         <button id="down" onClick="animation('down');">아래</button>
  57.     </div>
  58.    
  59. <script>
  60.    
  61.     $(function(){
  62.        
  63.         /* 변수설정 */
  64.         slides = $("#slide > li");
  65.         num = 0;
  66.         max = slides.length-1;
  67.         chk = true;
  68.         p = "";
  69.        
  70.         /* 기본위치 셋팅 */
  71.         slides.css("left",-500);
  72.         $(slides[num]).css("left",0);
  73.        
  74.         /* 타이틀 추가 */
  75.         $("#slide li").each(function() {
  76.             var title = $(this).attr("title");
  77.             var title_html = "<div class='title'>"+title+"</div>";
  78.             $(this).append(title_html);
  79.         });
  80.        
  81.         /* 위치버튼 추가 */
  82.         for(i=1;i<=max+1;i++){
  83.             p += "<div class='p' data-idx='"+(i-1)+"'></div>";
  84.         }
  85.         $("#numbers .wh").append(p);
  86.         $("#numbers .p:first-child").addClass("on");
  87.         $(".p").click(function(){
  88.             animation($(this).attr("data-idx"));
  89.         });
  90.        
  91.         /* 자동슬라이드 */
  92.         setInterval(function(){
  93.             animation('next');
  94.         },5000);
  95.        
  96.     });
  97.    
  98.     function animation(a){
  99.        
  100.         /* access */
  101.         if(!chk) return;
  102.         if(!isNaN(a) && a==num)return;
  103.        
  104.         /* 변수설정 */
  105.         chk=false;
  106.         var move;
  107.         var move2;
  108.        
  109.         /* 이동방향 설정 */
  110.         switch(a){
  111.             case 'prev':
  112.                 move=500;
  113.                 move2=-500;
  114.             break;
  115.             case 'up' :
  116.                 move=-300;
  117.                 move2=300;
  118.             break;
  119.             case 'down' :
  120.                 move=300;
  121.                 move2=-300;
  122.             break;
  123.             default:
  124.                 move=-500;
  125.                 move2=500;
  126.             break;
  127.         }
  128.        
  129.         /* 현재이미지 이동 */
  130.         if(a=='next'||a=='prev' || !isNaN(a)){
  131.             $(slides[num]).animate({left:move},500,function(){chk=true;});
  132.         } else {
  133.             $(slides[num]).animate({top:move},500,function(){chk=true;});
  134.         }
  135.        
  136.         /* 이동방향 */
  137.         if(a=='prev' || a=='down'){
  138.             if(--num<0) num=max;
  139.         } else {
  140.             if(++num>max)num=0;
  141.         }
  142.        
  143.         /* 위치버튼 클릭시 변수값변경 */
  144.         if(!isNaN(a)){
  145.             num=Number(a);
  146.         }
  147.        
  148.         /* 위치버튼 css설정 */
  149.         $(".p").removeClass("on");
  150.         $(".p[data-idx="+num+"]").addClass("on");
  151.        
  152.         /* 보여줄 이미지 이동 */
  153.         if(a=='next'||a=='prev' || !isNaN(a)){
  154.             $(slides[num]).css({left:move2,top:0}).animate({left:0},500);
  155.         } else {
  156.             $(slides[num]).css({top:move2,left:0}).animate({top:0},500);
  157.         }
  158.     }
  159.    
  160. </script>
  161. </body>
  162. </html>
Advertisement
Add Comment
Please, Sign In to add comment