asimryu

jquery slide 20141030-2

Oct 30th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.33 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="ko">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>slide</title>
  6. <style>
  7.     body{margin:0;}
  8.     #slidebox{position:relative;width:500px;height:300px;   margin:10px auto;overflow:hidden;}
  9.     #slide{width:500px;height:300px;overflow:hidden;list-style:none;    padding:0;margin:0;}
  10.     #slide li{position:absolute;left:0;top:0;}
  11.     #buttons, #numbers{width:500px;height:auto;text-align:center;   margin:0 auto;}
  12. </style>
  13. </head>
  14. <body>
  15.     <div id="slidebox">
  16.         <ul id="slide">
  17.             <li title="음식 이미지입니다."><img src="http://lorempixel.com/500/300/food"></li>
  18.             <li title="머신 자연입니다."><img src="http://lorempixel.com/500/300/nature"></li>
  19.             <li title="동물을 사랑합시다."><img src="http://lorempixel.com/500/300/animals"></li>
  20.             <li title="우리의 도시는 아름답습니다."><img src="http://lorempixel.com/500/300/city"></li>
  21.             <li title="열심히 운동합시다."><img src="http://lorempixel.com/500/300/sports"></li>
  22.         </ul>
  23.     </div>
  24.     <div id="numbers"></div>
  25.     <div id="buttons">
  26.         <button id="prev">이전</button>
  27.         <button id="next">다음</button>
  28.         <button id="up">위로</button>
  29.         <button id="down">아래로</button>
  30.         <button id="start">시작</button>
  31.         <button id="stop">중지</button>
  32.     </div>
  33.     <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
  34.     <script type="text/javascript">
  35.  
  36.         $(function(){
  37.  
  38.             $("#slide > li").css("left",-500);
  39.             var slides = $("#slide > li");
  40.             var sno = 0;
  41.             var max = slides.length - 1;
  42.             $(slides[sno]).css({left:0,top:0});
  43.             var isanimate = false;
  44.             var direction = "next";
  45.  
  46.             for( var i = 0; i <= max; i++ ) {
  47.                 var addhtml = " <a href='" + i + "'>" + (i+1) + "</a> ";
  48.                 $("#numbers").append( addhtml );
  49.             }  
  50.            
  51.             changeNumberColor();
  52.  
  53.             $("#numbers a").click(function(event){
  54.                 event.preventDefault();
  55.                 var tmpNo = $(this).attr("href");
  56.                 if(direction == "next" || direction == "dow"){
  57.                     sno = parseInt(tmpNo) - 1;
  58.                     if(sno < 0) sno = max;
  59.                 } else if(direction == "prev" || direction == "up"){
  60.                     sno = parseInt(tmpNo) + 1;
  61.                     if(sno >max) sno = 0;
  62.                 }
  63.                 isanimate = false;
  64.                 if( timer == null ) {
  65.                     $("#" + direction).click();
  66.                 }else{
  67.                     $("#stop").click();
  68.                     $("#" + direction).click();
  69.                     $("#start").click();
  70.                 }
  71.             });
  72.        
  73.             $("#next").click(function(){
  74.                 if( isanimate ) return;
  75.                 direction = "next";
  76.                 isanimate = true;
  77.                 $(slides[sno]).animate({left:500},1000,function(){isanimate=false;});
  78.                 sno++;
  79.                 if(sno>max) sno=0;
  80.                 $("#slide > li").css({left:-500,top:0});
  81.                 $(slides[sno]).animate({left:0},1000);
  82.                 changeNumberColor();
  83.             });
  84.  
  85.             $("#prev").click(function(){
  86.                 if( isanimate ) return;
  87.                 direction = "prev";
  88.                 isanimate = true;
  89.                 $(slides[sno]).animate({left:-500},1000,function(){isanimate=false;});
  90.                 sno--;
  91.                 if(sno<0) sno=max;
  92.                 $("#slide > li").css({left:500,top:0});
  93.                 $(slides[sno]).animate({left:0},1000);
  94.                 changeNumberColor();
  95.             });
  96.  
  97.             $("#up").click(function(){
  98.                 if( isanimate ) return;
  99.                 direction = "up";
  100.                 isanimate = true;
  101.                 $(slides[sno]).animate({top:-300},1000,function(){isanimate=false;});
  102.                 sno++;
  103.                 if(sno>max) sno=0;
  104.                 $("#slide > li").css({left:0,top:300});
  105.                 $(slides[sno]).animate({top:0},1000);
  106.                 changeNumberColor();
  107.             });
  108.  
  109.             $("#down").click(function(){
  110.                 if( isanimate ) return;
  111.                 direction = "down";
  112.                 isanimate = true;
  113.                 $(slides[sno]).animate({top:300},1000,function(){isanimate=false;});
  114.                 sno--;
  115.                 if(sno<0) sno=max;
  116.                 $("#slide > li").css({left:0,top:-300});
  117.                 $(slides[sno]).animate({top:0},1000);
  118.                 changeNumberColor();
  119.             });
  120.  
  121.             function changeNumberColor(){
  122.                 var slidelinks = $("#numbers a");
  123.                 for(var i = 0; i < slidelinks.length; i++){
  124.                     if(i == sno) {
  125.                         $(slidelinks[i]).css({color:"red",fontWeight:"bold"});
  126.                     } else {
  127.                         $(slidelinks[i]).css({color:"black",fontWeight:"normal"});
  128.                     }
  129.                 }
  130.             }
  131.  
  132.             var timer = null;
  133.             $("#start").click(function(){
  134.                 if( timer == null ) {
  135.                     timer = setInterval(function(){ $("#" + direction).click(); },3000);
  136.                 }
  137.             });
  138.  
  139.             $("#stop").click(function(){
  140.                 if( timer != null ) {
  141.                     clearInterval(timer);
  142.                     timer = null;
  143.                 }
  144.             });
  145.  
  146.             $("#start").click();
  147.  
  148.         });
  149.  
  150.     </script>
  151. </body>
  152. </html>
Advertisement
Add Comment
Please, Sign In to add comment