asimryu

slide with jquery animate

Jun 11th, 2014
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.79 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <title>Document</title>
  5.     <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  6.     <style>
  7.         #slide { position:relative; width:900px; height:300px; overflow:hidden; }
  8.         #slide img{ position:absolute; top:0; left:0;}
  9.         .prevnext { position:absolute; top:135px; left:0; width:100%; height:30px;}
  10.         .prevnext div { width:50px; height:30px; background:#fff; line-height:30px;text-align:center; opacity:0.5; cursor:pointer;}
  11.         .prevnext div:hover {color:red;}
  12.         .prevnext .prev { float:left;}
  13.         .prevnext .next { float:right;}
  14.     </style>
  15. </head>
  16. <body>
  17.     <div id="slide">
  18.         <img id="s1" src="http://lorempixel.com/900/300/city">
  19.         <img id="s2" src="http://lorempixel.com/900/300/food">
  20.         <img id="s3" src="http://lorempixel.com/900/300/nature">
  21.         <img id="s4" src="http://lorempixel.com/900/300/animals">
  22.         <img id="s5" src="http://lorempixel.com/900/300/business">
  23.         <div class="prevnext">
  24.             <div class="prev">Prev</div>
  25.             <div class="next">Next</div>
  26.         </div>
  27.     </div>
  28.     <script>
  29.         $("#slide img").css("left","-900px");
  30.         $("#s1").css("left","0");
  31.         var sno = 1;
  32.         function fade(num) {
  33.                 if(num < 1) {
  34.                     $("#s" + sno).animate({left: "-900px"}, 500);
  35.                 } else {
  36.                     $("#s" + sno).animate({left: "900px"}, 500);
  37.                 }
  38.  
  39.                 sno = sno + num;
  40.                 if(sno > 5) sno = 1;
  41.                 if(sno < 1) sno = 5;
  42.                 if(num < 1) {
  43.                     $("#s" + sno).css("left","900px");
  44.                 } else {
  45.                     $("#s" + sno).css("left","-900px");
  46.                 }
  47.                 $("#s" + sno).animate({left: "0"}, 500);
  48.         }
  49.         $(".prev").click( function(){fade(-1);} );
  50.         $(".next").click( function(){fade(1);} );
  51.         //아래 줄의 주석을 제거하면 자동으로 매 2초마다 슬라이드 재생됨.
  52.         //var timer = setInterval(function(){fade(1);},2000);
  53.     </script>
  54. </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment