asimryu

simple slide example 6(2015.06.16) 위아래

Jun 16th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.43 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <style>
  6.     .wrap {
  7.         width: 300px;
  8.         margin: 0 auto;
  9.     }
  10.     #slider {
  11.         width: 300px;
  12.         height: 200px;
  13.         position: relative;
  14.         list-style: none;
  15.         margin: 0;
  16.         padding: 0;
  17.         overflow: hidden;
  18.     }
  19.     #slider > li {
  20.         width:300px;
  21.         height: 200px;
  22.         position: absolute;
  23.         left: 0;
  24.         top: 0;
  25.     }
  26. </style>
  27. <script src="http://code.jquery.com/jquery-latest.js"></script>
  28. </head>
  29. <body>
  30. <div class="wrap">
  31.     <h1>Image Slide</h1>
  32.     <ul id="slider">
  33.         <li><img src="http://lorempixel.com/300/200/nature"></li>
  34.         <li><img src="http://lorempixel.com/300/200/sports"></li>
  35.         <li><img src="http://lorempixel.com/300/200/cats"></li>
  36.         <li><img src="http://lorempixel.com/300/200/city"></li>
  37.         <li><img src="http://lorempixel.com/300/200/food"></li>
  38.     </ul>
  39.     <button id="start">시작</button>
  40.     <button id="stop">중지</button>
  41. </div>
  42. <script>
  43.     var timer = null;
  44.     var slides = $("#slider > li");
  45.     var first = 0;
  46.     var max = slides.length - 1;
  47.     var dir = "right";
  48.  
  49.     $("#slider > li").css({left:"-300px"});
  50.     $(slides[first]).animate({left:"0"},1000);
  51.     $(function(){
  52.  
  53.         $("#slider").click(function(){
  54.             if( dir == "right" ) {
  55.                 var to = "300px";
  56.                 var from = "-300px";
  57.             } else if( dir == "left") {
  58.                 var to = "-300px";
  59.                 var from = "300px";
  60.             } else if( dir == "up") {
  61.                 //
  62.                 //
  63.             } else if( dir == "down") {
  64.                 //
  65.                 //
  66.             }
  67.  
  68.             $(slides[first]).animate({left:to},1000,function(){
  69.                 $(this).css({left:from});
  70.             });
  71.             first = first + 1; //first++
  72.             if( first > max ) first = 0;
  73.             $(slides[first]).css({left:from});
  74.             $(slides[first]).animate({left:"0"},1000);
  75.         });
  76.  
  77.         $("#start").click(function(){
  78.             if( timer == null ) {
  79.                 timer = setInterval(function(){
  80.                     $("#slider").click();
  81.                 },2000);
  82.             }
  83.         });
  84.  
  85.         $("#stop").click(function(){
  86.             if( timer != null ) {
  87.                 clearInterval(timer);
  88.                 timer = null;
  89.             }
  90.         });
  91.  
  92.     });
  93.  
  94. $(document).keydown(function(e) {
  95.     switch(e.which) {
  96.         case 37: // left
  97.                                 dir = "left";
  98.                                 $("#slider").click();
  99.         break;
  100.         case 38: // up
  101.         break;
  102.         case 39: // right
  103.                                 dir = "right";
  104.                                 $("#slider").click();
  105.         break;
  106.         case 40: // down
  107.         break;
  108.         default: return; // exit this handler for other keys
  109.     }
  110.     e.preventDefault(); // prevent the default action (scroll / move caret)
  111. });
  112.  
  113. </script>
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment