(function($){ $.gramSlider = function(el, options){ // To avoid scope issues, use 'base' instead of 'this' // to reference this class from internal events and functions. var base = this; // Access to jQuery and DOM versions of element base.$el = $(el); base.el = el; // Add a reverse reference to the DOM object base.$el.data("gramSlider", base); // Obliczanie długości base.calculateWidths = function(){ base.$sliderWrapperWidth = base.$sliderWrapper.width(); base.$sliderInnerWidth = base.$el.width(); base.maxMove = base.$sliderInnerWidth - base.$sliderWrapperWidth; } // Init base.init = function(){ base.options = $.extend({},$.gramSlider.defaultOptions, options); base.$el.wrap('
'); base.$sliderWrapper = base.$el.parent(); base.$prev = $('<'); base.$next = $('>'); base.$sliderWrapper.before(base.$prev); base.$sliderWrapper.before(base.$next); base.$el.css('left', '0px'); base.movedBy = 0; base.calculateWidths(); //jesli przesuwamy do konca if (base.options["movedToEnd"] === true && base.$sliderWrapperWidth < base.$sliderInnerWidth) { base.movedBy = base.maxMove; base.$el.css('left', -base.movedBy); base.$prev.addClass('visible'); } else if ( base.$sliderWrapperWidth < base.$sliderInnerWidth ) base.$next.addClass('visible'); }; // Przewijanie do przodu base.next = function(){ base.movedBy += base.$sliderWrapperWidth - 90; if ( base.movedBy > base.maxMove ) { base.movedBy = base.maxMove; base.$next.removeClass('visible'); base.$prev.addClass('visible'); } else { base.$next.addClass('visible'); base.$prev.addClass('visible'); } base.$el.css('left', -base.movedBy); }; // Przewijanie do tyłu base.prev = function(){ base.movedBy -= base.$sliderWrapperWidth + 90; if ( base.movedBy < 0 ) { base.movedBy = 0; base.$prev.removeClass('visible'); base.$next.addClass('visible'); } else { base.$prev.addClass('visible'); base.$next.addClass('visible'); } base.$el.css('left', -base.movedBy); }; // Run initializer base.init(); base.$next.click(function(){ base.next(); }); base.$prev.click(function(){ base.prev(); }); }; $.gramSlider.defaultOptions = { arrowWidth: 90, movedToEnd: false, }; $.fn.gramSlider = function(options){ return this.each(function(){ (new $.gramSlider(this, options)); }); }; })(jQuery);