asimryu

simple slide example 5(2015.06.16)

Jun 16th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.65 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.     $("#slider > li").css({left:"-300px"});
  48.     $(slides[first]).animate({left:"0"},1000);
  49.     $(function(){
  50.  
  51.         $("#slider").click(function(){
  52.             $(slides[first]).animate({left:"300px"},1000,function(){
  53.                 $(this).css({left:"-300px"});
  54.             });
  55.             first = first + 1; //first++
  56.             if( first > max ) first = 0;
  57.             $(slides[first]).animate({left:"0"},1000);
  58.         });
  59.  
  60.         $("#start").click(function(){
  61.             if( timer == null ) {
  62.                 timer = setInterval(function(){
  63.                     $("#slider").click();
  64.                 },2000);
  65.             }
  66.         });
  67.  
  68.         $("#stop").click(function(){
  69.             if( timer != null ) {
  70.                 clearInterval(timer);
  71.                 timer = null;
  72.             }
  73.         });
  74.  
  75.     });
  76.  
  77. </script>
  78. </body>
  79. </html>
Advertisement
Add Comment
Please, Sign In to add comment