asimryu

slider basic

Aug 23rd, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.28 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>slide</title>
  6.     <style>
  7.         #slider {
  8.             width: 800px;
  9.             height: 400px;
  10.             margin: 0 auto;
  11.             position: relative;
  12.             background-color: pink;
  13.             overflow: hidden;
  14.         }
  15.         #slider > img {
  16.             position: absolute;
  17.             top: 0;
  18.             left: -800px;
  19.         }
  20.         #slider > img:first-child {
  21.             left: 0;
  22.         }
  23.     </style>
  24. </head>
  25. <body>
  26.     <div id="slider">
  27.         <img src="food.jpg" width="800" height="400" alt="food">
  28.         <img src="cats.jpg" width="800" height="400" alt="cats">
  29.         <img src="city.jpg" width="800" height="400" alt="city">
  30.         <img src="fashion.jpg" width="800" height="400" alt="fashion">
  31.         <img src="sports.jpg" width="800" height="400" alt="sports">
  32.     </div>
  33.     <script src="http://code.jquery.com/jquery-latest.js"></script>
  34.     <script>
  35.         var imgs = $("#slider > img");
  36.         var last = imgs.length - 1;
  37.         var sno = 0;
  38.         $("#slider").on("click",function(){
  39.             if( $(imgs[sno]).is(":animated") ) return;
  40.             $(imgs[sno]).animate({"left":"800px"},1000,function(){
  41.                     $(this).css({"left":"-800px"});
  42.             });
  43.             sno++; //sno = sno + 1;
  44.             if( sno > last ) sno = 0;
  45.             $(imgs[sno]).animate({"left":"0"},1000);
  46.         });
  47.         var timer = setInterval(function(){
  48.             $("#slider").click();
  49.         },2000);
  50.     </script>
  51. </body>
  52. </html>
Advertisement
Add Comment
Please, Sign In to add comment