Advertisement
Guest User

jQuery

a guest
Oct 27th, 2014
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /*
  2.      * jQuery Cycle Carousel plugin
  3.      */
  4.     ;(function($){
  5.         function ScrollAbsoluteGallery(options) {
  6.             this.options = $.extend({
  7.                 activeClass: 'active',
  8.                 mask: 'div.slides-mask',
  9.                 slider: '>ul',
  10.                 slides: '>li',
  11.                 btnPrev: '.btn-prev',
  12.                 btnNext: '.btn-next',
  13.                 pagerLinks: 'ul.pager > li',
  14.                 generatePagination: false,
  15.                 pagerList: '<ul>',
  16.                 pagerListItem: '<li><a href="#"></a></li>',
  17.                 pagerListItemText: 'a',
  18.                 galleryReadyClass: 'gallery-js-ready',
  19.                 currentNumber: 'span.current-num',
  20.                 totalNumber: 'span.total-num',
  21.                 maskAutoSize: false,
  22.                 autoRotation: false,
  23.                 pauseOnHover: false,
  24.                 stretchSlideToMask: false,
  25.                 switchTime: 3000,
  26.                 animSpeed: 500,
  27.                 handleTouch: true,
  28.                 swipeThreshold: 15,
  29.                 vertical: false
  30.             }, options);
  31.             this.init();
  32.         }
  33.         ScrollAbsoluteGallery.prototype = {
  34.             init: function() {
  35.                 if(this.options.holder) {
  36.                     this.findElements();
  37.                     this.attachEvents();
  38.                     this.makeCallback('onInit', this);
  39.                 }
  40.             },
  41.             findElements: function() {
  42.                 // find structure elements
  43.                 this.holder = $(this.options.holder).addClass(this.options.galleryReadyClass);
  44.                 this.mask = this.holder.find(this.options.mask);
  45.                 this.slider = this.mask.find(this.options.slider);
  46.                 this.slides = this.slider.find(this.options.slides);
  47.                 this.btnPrev = this.holder.find(this.options.btnPrev);
  48.                 this.btnNext = this.holder.find(this.options.btnNext);
  49.    
  50.                 // slide count display
  51.                 this.currentNumber = this.holder.find(this.options.currentNumber);
  52.                 this.totalNumber = this.holder.find(this.options.totalNumber);
  53.    
  54.                 // create gallery pagination
  55.                 if(typeof this.options.generatePagination === 'string') {
  56.                     this.pagerLinks = this.buildPagination();
  57.                 } else {
  58.                     this.pagerLinks = this.holder.find(this.options.pagerLinks);
  59.                 }
  60.    
  61.                 // define index variables
  62.                 this.sizeProperty = this.options.vertical ? 'height' : 'width';
  63.                 this.positionProperty = this.options.vertical ? 'top' : 'left';
  64.                 this.animProperty = this.options.vertical ? 'marginTop' : 'marginLeft';
  65.    
  66.                 this.slideSize = this.slides[this.sizeProperty]();
  67.                 this.currentIndex = 0;
  68.                 this.prevIndex = 0;
  69.    
  70.                 // reposition elements
  71.                 this.options.maskAutoSize = this.options.vertical ? false : this.options.maskAutoSize;
  72.                 if(this.options.vertical) {
  73.                     this.mask.css({
  74.                         height: this.slides.innerHeight()
  75.                     });
  76.                 }
  77.                 if(this.options.maskAutoSize){
  78.                     this.mask.css({
  79.                         height: this.slider.height()
  80.                     });
  81.                 }
  82.                 this.slider.css({
  83.                     position: 'relative',
  84.                     height: this.options.vertical ? this.slideSize * this.slides.length : '100%'
  85.                 });
  86.                 this.slides.css({
  87.                     position: 'absolute'
  88.                 }).css(this.positionProperty, -9999).eq(this.currentIndex).css(this.positionProperty, 0);
  89.                 this.refreshState();
  90.             },
  91.             buildPagination: function() {
  92.                 var pagerLinks = $();
  93.                 if(!this.pagerHolder) {
  94.                     this.pagerHolder = this.holder.find(this.options.generatePagination);
  95.                 }
  96.                 if(this.pagerHolder.length) {
  97.                     this.pagerHolder.empty();
  98.                     this.pagerList = $(this.options.pagerList).appendTo(this.pagerHolder);
  99.                     for(var i = 0; i < this.slides.length; i++) {
  100.                         $(this.options.pagerListItem).appendTo(this.pagerList).find(this.options.pagerListItemText).text(i+1);
  101.                     }
  102.                     pagerLinks = this.pagerList.children();
  103.                 }
  104.                 return pagerLinks;
  105.             },
  106.             attachEvents: function() {
  107.                 // attach handlers
  108.                 var self = this;
  109.                 if(this.btnPrev.length) {
  110.                     this.btnPrevHandler = function(e) {
  111.                         e.preventDefault();
  112.                         self.prevSlide();
  113.                     };
  114.                     this.btnPrev.click(this.btnPrevHandler);
  115.                 }
  116.                 if(this.btnNext.length) {
  117.                     this.btnNextHandler = function(e) {
  118.                         e.preventDefault();
  119.                         self.nextSlide();
  120.                     };
  121.                     this.btnNext.click(this.btnNextHandler);
  122.                 }
  123.                 if(this.pagerLinks.length) {
  124.                     this.pagerLinksHandler = function(e) {
  125.                         e.preventDefault();
  126.                         self.numSlide(self.pagerLinks.index(e.currentTarget));
  127.                     };
  128.                     this.pagerLinks.click(this.pagerLinksHandler);
  129.                 }
  130.    
  131.                 // handle autorotation pause on hover
  132.                 if(this.options.pauseOnHover) {
  133.                     this.hoverHandler = function() {
  134.                         clearTimeout(self.timer);
  135.                     };
  136.                     this.leaveHandler = function() {
  137.                         self.autoRotate();
  138.                     };
  139.                     this.holder.bind({mouseenter: this.hoverHandler, mouseleave: this.leaveHandler});
  140.                 }
  141.    
  142.                 // handle holder and slides dimensions
  143.                 this.resizeHandler = function() {
  144.                     if(!self.animating) {
  145.                         if(self.options.stretchSlideToMask) {
  146.                             self.resizeSlides();
  147.                         }
  148.                         self.resizeHolder();
  149.                         self.setSlidesPosition(self.currentIndex);
  150.                     }
  151.                 };
  152.                 $(window).bind('load resize orientationchange', this.resizeHandler);
  153.                 if(self.options.stretchSlideToMask) {
  154.                     self.resizeSlides();
  155.                 }
  156.    
  157.                 // handle swipe on mobile devices
  158.                 if(this.options.handleTouch && window.Hammer && this.mask.length && this.slides.length > 1 && isTouchDevice) {
  159.                     this.swipeHandler = new Hammer.Manager(this.mask[0]);
  160.                     this.swipeHandler.add(new Hammer.Pan({
  161.                         direction: self.options.vertical ? Hammer.DIRECTION_VERTICAL : Hammer.DIRECTION_HORIZONTAL,
  162.                         threshold: self.options.swipeThreshold
  163.                     }));
  164.    
  165.                     this.swipeHandler.on('panstart', function() {
  166.                         if(self.animating) {
  167.                             self.swipeHandler.stop();
  168.                         } else {
  169.                             clearTimeout(self.timer);
  170.                         }
  171.                     }).on('panmove', function(e) {
  172.                         self.swipeOffset = -self.slideSize + e[self.options.vertical ? 'deltaY' : 'deltaX'];
  173.                         self.slider.css(self.animProperty, self.swipeOffset);
  174.                         clearTimeout(self.timer);
  175.                     }).on('panend', function(e) {
  176.                         if(e.distance > self.options.swipeThreshold) {
  177.                             if(e.offsetDirection === Hammer.DIRECTION_RIGHT || e.offsetDirection === Hammer.DIRECTION_DOWN) {
  178.                                 self.nextSlide();
  179.                             } else {
  180.                                 self.prevSlide();
  181.                             }
  182.                         } else {
  183.                             var tmpObj = {};
  184.                             tmpObj[self.animProperty] = -self.slideSize;
  185.                             self.slider.animate(tmpObj, {duration: self.options.animSpeed});
  186.                             self.autoRotate();
  187.                         }
  188.                         self.swipeOffset = 0;
  189.                     });
  190.                 }
  191.    
  192.                 // start autorotation
  193.                 this.autoRotate();
  194.                 this.resizeHolder();
  195.                 this.setSlidesPosition(this.currentIndex);
  196.             },
  197.             resizeSlides: function() {
  198.                 this.slideSize = this.mask[this.options.vertical ? 'height' : 'width']();
  199.                 this.slides.css(this.sizeProperty, this.slideSize);
  200.             },
  201.             resizeHolder: function() {
  202.                 if(this.options.maskAutoSize) {
  203.                     this.mask.css({
  204.                         height: this.slides.eq(this.currentIndex).outerHeight(true)
  205.                     });
  206.                 }
  207.             },
  208.             prevSlide: function() {
  209.                 if(!this.animating && this.slides.length > 1) {
  210.                     this.direction = -1;
  211.                     this.prevIndex = this.currentIndex;
  212.                     if(this.currentIndex > 0) this.currentIndex--;
  213.                     else this.currentIndex = this.slides.length - 1;
  214.                     this.switchSlide();
  215.                 }
  216.             },
  217.             nextSlide: function(fromAutoRotation) {
  218.                 if(!this.animating && this.slides.length > 1) {
  219.                     this.direction = 1;
  220.                     this.prevIndex = this.currentIndex;
  221.                     if(this.currentIndex < this.slides.length - 1) this.currentIndex++;
  222.                     else this.currentIndex = 0;
  223.                     this.switchSlide();
  224.                 }
  225.             },
  226.             numSlide: function(c) {
  227.                 if(!this.animating && this.currentIndex !== c && this.slides.length > 1) {
  228.                     this.direction = c > this.currentIndex ? 1 : -1;
  229.                     this.prevIndex = this.currentIndex;
  230.                     this.currentIndex = c;
  231.                     this.switchSlide();
  232.                 }
  233.             },
  234.             preparePosition: function() {
  235.                 // prepare slides position before animation
  236.                 this.setSlidesPosition(this.prevIndex, this.direction < 0 ? this.currentIndex : null, this.direction > 0 ? this.currentIndex : null, this.direction);
  237.             },
  238.             setSlidesPosition: function(index, slideLeft, slideRight, direction) {
  239.                 // reposition holder and nearest slides
  240.                 if(this.slides.length > 1) {
  241.                     var prevIndex = (typeof slideLeft === 'number' ? slideLeft : index > 0 ? index - 1 : this.slides.length - 1);
  242.                     var nextIndex = (typeof slideRight === 'number' ? slideRight : index < this.slides.length - 1 ? index + 1 : 0);
  243.    
  244.                     this.slider.css(this.animProperty, this.swipeOffset ? this.swipeOffset : -this.slideSize);
  245.                     this.slides.css(this.positionProperty, -9999).eq(index).css(this.positionProperty, this.slideSize);
  246.                     if(prevIndex === nextIndex && typeof direction === 'number') {
  247.                         var calcOffset = direction > 0 ? this.slideSize*2 : 0;
  248.                         this.slides.eq(nextIndex).css(this.positionProperty, calcOffset);
  249.                     } else {
  250.                         this.slides.eq(prevIndex).css(this.positionProperty, 0);
  251.                         this.slides.eq(nextIndex).css(this.positionProperty, this.slideSize*2);
  252.                     }
  253.                 }
  254.             },
  255.             switchSlide: function() {
  256.                 // prepare positions and calculate offset
  257.                 var self = this;
  258.                 var oldSlide = this.slides.eq(this.prevIndex);
  259.                 var newSlide = this.slides.eq(this.currentIndex);
  260.                 this.animating = true;
  261.    
  262.                 // resize mask to fit slide
  263.                 if(this.options.maskAutoSize) {
  264.                     this.mask.animate({
  265.                         height: newSlide.outerHeight(true)
  266.                     }, {
  267.                         duration: this.options.animSpeed
  268.                     });
  269.                 }
  270.    
  271.                 // start animation
  272.                 var animProps = {};
  273.                 animProps[this.animProperty] = this.direction > 0 ? -this.slideSize*2 : 0;
  274.                 this.preparePosition();
  275.                 this.slider.animate(animProps,{duration:this.options.animSpeed, complete:function() {
  276.                     self.setSlidesPosition(self.currentIndex);
  277.    
  278.                     // start autorotation
  279.                     self.animating = false;
  280.                     self.autoRotate();
  281.    
  282.                     // onchange callback
  283.                     self.makeCallback('onChange', self);
  284.                 }});
  285.    
  286.                 // refresh classes
  287.                 this.refreshState();
  288.    
  289.                 // onchange callback
  290.                 this.makeCallback('onBeforeChange', this);
  291.             },
  292.             refreshState: function(initial) {
  293.                 // slide change function
  294.                 this.slides.removeClass(this.options.activeClass).eq(this.currentIndex).addClass(this.options.activeClass);
  295.                 this.pagerLinks.removeClass(this.options.activeClass).eq(this.currentIndex).addClass(this.options.activeClass);
  296.    
  297.                 // display current slide number
  298.                 this.currentNumber.html(this.currentIndex + 1);
  299.                 this.totalNumber.html(this.slides.length);
  300.    
  301.                 // add class if not enough slides
  302.                 this.holder.toggleClass('not-enough-slides', this.slides.length === 1);
  303.             },
  304.             autoRotate: function() {
  305.                 var self = this;
  306.                 clearTimeout(this.timer);
  307.                 if(this.options.autoRotation) {
  308.                     this.timer = setTimeout(function() {
  309.                         self.nextSlide();
  310.                     }, this.options.switchTime);
  311.                 }
  312.             },
  313.             makeCallback: function(name) {
  314.                 if(typeof this.options[name] === 'function') {
  315.                     var args = Array.prototype.slice.call(arguments);
  316.                     args.shift();
  317.                     this.options[name].apply(this, args);
  318.                 }
  319.             },
  320.             destroy: function() {
  321.                 // destroy handler
  322.                 this.btnPrev.unbind('click', this.btnPrevHandler);
  323.                 this.btnNext.unbind('click', this.btnNextHandler);
  324.                 this.pagerLinks.unbind('click', this.pagerLinksHandler);
  325.                 this.holder.unbind('mouseenter', this.hoverHandler);
  326.                 this.holder.unbind('mouseleave', this.leaveHandler);
  327.                 $(window).unbind('load resize orientationchange', this.resizeHandler);
  328.                 clearTimeout(this.timer);
  329.    
  330.                 // destroy swipe handler
  331.                 if(this.swipeHandler) {
  332.                     this.swipeHandler.destroy();
  333.                 }
  334.    
  335.                 // remove inline styles, classes and pagination
  336.                 this.holder.removeClass(this.options.galleryReadyClass);
  337.                 this.slider.add(this.slides).removeAttr('style');
  338.                 if(typeof this.options.generatePagination === 'string') {
  339.                     this.pagerHolder.empty();
  340.                 }
  341.             }
  342.         };
  343.    
  344.         // detect device type
  345.         var isTouchDevice = /Windows Phone/.test(navigator.userAgent) || ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
  346.    
  347.         // jquery plugin
  348.         $.fn.scrollAbsoluteGallery = function(opt){
  349.             return this.each(function(){
  350.                 $(this).data('ScrollAbsoluteGallery', new ScrollAbsoluteGallery($.extend(opt,{holder:this})));
  351.             });
  352.         };
  353.     }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement