Advertisement
gufoe

JQuery Image Slider

Feb 3rd, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.38 KB | None | 0 0
  1. <!--
  2. Author: Giacomo Rizzi (gcmrzz@gmail.com)
  3. Date: 3rd feb 2014
  4. -->
  5.  
  6. <!doctype html>
  7. <html lang="en">
  8.     <head>
  9.         <title>JQuery image slider</title>
  10.         <meta charset="UTF-8"/>
  11.         <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  12.        
  13.         <style>
  14.             #slider {
  15.                 width: 800px;
  16.                 height: 500px;
  17.                 overflow: hidden;
  18.                 text-align: center;
  19.                 background: #000;
  20.                 position: relative;
  21.             }
  22.            
  23.             #slider img {
  24.                 display: none;
  25.             }
  26.            
  27.             #slider img.show {
  28.                 display: block;
  29.                 max-width: 100%;
  30.                 max-height: 100%;
  31.                 margin: auto auto;
  32.                 position: absolute;
  33.                 top: 0;
  34.                 bottom: 0;
  35.                 left: 0;
  36.                 right: 0;
  37.             }
  38.            
  39.         </style>
  40.     </head>
  41.     <body>
  42.        
  43.        
  44.         <div id="slider">
  45.             <img src="img1.jpg" alt="img1">
  46.             <img src="img2.jpg" alt="img2">
  47.             <img src="img3.jpg" alt="img3">
  48.             <img src="img4.jpg" alt="img4">
  49.             <!-- And so on... -->
  50.         </div>
  51.        
  52.         <script>
  53.            
  54.             var slider = $('#slider'),
  55.                 images = slider.children('img'),
  56.                
  57.                 // The image timeout in milliseconds
  58.                 timeout = 1000,
  59.                
  60.                 // The animation timeout in milliseconds
  61.                 animation = 400;
  62.            
  63.            
  64.             images.first().addClass('show');
  65.            
  66.             setInterval(function() {
  67.                
  68.                 var current = slider.find('.show');
  69.                 var next = current.next('img');
  70.                
  71.                 if (next.attr('src') == null) {
  72.                     next = images.first();
  73.                 }
  74.                 next.css('opacity', 0);
  75.                 next.first().addClass('show');
  76.                
  77.                 next.animate({'opacity' : 1}, animation);
  78.                 current.animate({'opacity' : 0}, animation, function() {
  79.                     current.removeClass('show')
  80.                 })
  81.                
  82.             }, timeout);
  83.            
  84.         </script>
  85.     </body>
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement