Advertisement
asimryu

slider Left/Right/Up/Down Buttons

Dec 19th, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.33 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>slider</title>
  6.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
  7.     <style>
  8.         .wrap { width: 600px; margin: 0 auto; }
  9.         h2 { text-align: center; }
  10.         .slider { width: 100%; height: 300px; overflow: hidden; margin-bottom: 30px; position: relative;}
  11.         .slider img { width: 100%; height: 300px; position: absolute; top: 0; left: 0; }
  12.     </style>
  13. </head>
  14. <body>
  15.  
  16.     <div class="wrap">
  17.         <h2>Slider: Left/Right/Up/Down Buttons </h2>
  18.         <p>
  19.             <button onclick="playSlide('left')"><i class="fas fa-chevron-left"></i></button>
  20.             <button onclick="playSlide('right')"><i class="fas fa-chevron-right"></i></button>
  21.             <button onclick="playSlide('up')"><i class="fas fa-chevron-up"></i></button>
  22.             <button onclick="playSlide('down')"><i class="fas fa-chevron-down"></i></button>
  23.         </p>
  24.         <div class="slider">
  25.             <img src="https://placeimg.com/600/300/nature" alt="nature">
  26.             <img src="https://placeimg.com/600/300/animals" alt="nature">
  27.             <img src="https://placeimg.com/600/300/arch" alt="arch">
  28.             <img src="https://placeimg.com/600/300/people" alt="people">
  29.             <img src="https://placeimg.com/600/300/tech" alt="tech">
  30.         </div>
  31.     </div>
  32.  
  33.     <script src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
  34.     <script>
  35.         var slide = $(".slider img");
  36.         var sno = 0;
  37.  
  38.         function playSlide(dir){
  39.             var left_start = null, left_end = null, top_start = null, top_end = null;
  40.             if( dir == "right" ) {
  41.                 left_start = "-100%";
  42.                 left_end = "100%";
  43.                 top_start = 0;
  44.                 top_end = 0;
  45.             } else if ( dir == "left" ) {
  46.                 left_start = "100%";
  47.                 left_end = "-100%";
  48.                 top_start = 0;
  49.                 top_end = 0;
  50.             } else if ( dir == "up" ) {
  51.                 left_start = 0;
  52.                 left_end = 0;
  53.                 top_start = "100%";
  54.                 top_end = "-100%";
  55.             } else if ( dir == "down" ) {
  56.                 left_start = 0;
  57.                 left_end = 0;
  58.                 top_start = "-100%";
  59.                 top_end = "100%";
  60.             } else {
  61.                 return;
  62.             }
  63.             if( slide.length == 0 ) return;
  64.             if( $(slide[sno]).is(":animated") ) return;
  65.             $(slide[sno]).siblings("img").css({left: left_start, top: top_start});
  66.             $(slide[sno]).animate({left: left_end, top: top_end},1000);
  67.             sno++;
  68.             if( sno >= slide.length ) sno = 0;
  69.             $(slide[sno]).animate({left: 0, top: 0},1000);
  70.         }
  71.  
  72.     </script>
  73. </body>
  74. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement