Advertisement
Guest User

Slider JS

a guest
Jul 22nd, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function(){
  2.     // #########################
  3.     // ## Slider and Loader
  4.     // #########################
  5.  
  6.     var currentSlide = 1;   // Loader
  7.     var totalWidth = 0;
  8.     var timer;
  9.     var slides = [];
  10.  
  11.     if (jQuery('#main > .wrapper').width() < 480) {
  12.         return;
  13.     }
  14.  
  15.     // Set the first slide as active
  16.     jQuery("#slider img:first").addClass("slide-active");
  17.  
  18.     function loadWidths() {
  19.         var notLoaded = false;
  20.         totalWidth = 0;
  21.         jQuery(".slide").each(function(i) {
  22.             var $this = jQuery(this);
  23.             var slideWidth = $this.outerWidth(true);
  24.             slides[i] = {
  25.                 'position' : totalWidth + 1,
  26.                 'slide' : $this
  27.             };
  28.             totalWidth = totalWidth + slideWidth;
  29.             if (slideWidth < 1) {
  30.                 notLoaded = true;
  31.             }
  32.         });
  33.  
  34.         if (notLoaded) {
  35.             setTimeout(loadWidths, 599);
  36.             return;
  37.         }
  38.  
  39.         // Set the gallery width to the totalWidth so all items are on same line
  40.         jQuery("#slider").width(totalWidth + 10); //added 10 because sometimes the final slide doesn't fit for some reason
  41.  
  42.         // All slides loaded so create timer to auto-advance slides
  43.         timer = window.setInterval(function() {
  44.             jQuery(".button-next").trigger('click');
  45.         }, 4000);
  46.     }
  47.  
  48.     function shiftSlide(targetItem) {
  49.         // Make sure the item exists
  50.         if (targetItem) {
  51.             // Clear the timer
  52.             if (timer)
  53.                 clearInterval(timer);
  54.  
  55.             // The new position is just to the left of the targetItem
  56.             var newPosition = targetItem.position;
  57.  
  58.             // Add active class to the target item
  59.             targetItem.slide.addClass("slide-active");
  60.  
  61.             // Remove the active class from all other items
  62.             targetItem.slide.siblings().removeClass("slide-active");
  63.  
  64.             // Animate slide to the correct position
  65.             jQuery("#slider").animate({
  66.                 left : - newPosition
  67.             });
  68.  
  69.             // Restart the timer
  70.             timer = window.setInterval(function() {
  71.                 jQuery(".button-next").trigger('click');
  72.             }, 4000);
  73.  
  74.         }
  75.     }
  76.  
  77.     // Prev button click
  78.     jQuery(".button-prev").click(function(e){
  79.         e.preventDefault();
  80.         var targetItem;
  81.         if (currentSlide <= 1) {
  82.             // Reset to last slide
  83.             targetItem = slides[slides.length - 1];
  84.             currentSlide = slides.length;
  85.         } else {
  86.             // Set target item to the item before the active item
  87.             currentSlide -= 1;
  88.             targetItem = slides[currentSlide - 1];
  89.         }
  90.         shiftSlide(targetItem);
  91.     });
  92.  
  93.     // Next button click
  94.     jQuery(".button-next").click(function(e){
  95.         e.preventDefault();
  96.         var targetItem;
  97.         if (currentSlide == slides.length) {
  98.             // Reset to first slide
  99.             targetItem = slides[0];
  100.             currentSlide = 1;
  101.         } else {
  102.             // Set target item to the item after the active item
  103.             targetItem = slides[currentSlide];
  104.             currentSlide += 1;
  105.         }
  106.         shiftSlide(targetItem);
  107.     });
  108.  
  109.     loadWidths();
  110. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement