- (function(namespace) {
- "use strict";
- var Scroll = function(element, options) {
- this.defaultOptions = {
- dots: false,
- arrows: false,
- infinite: true,
- speed: 300,
- swipeToSlide: true,
- slidesToShow: 1,
- initialSlide: 1,
- variableWidth: true,
- touchThreshold: 999
- };
- this.element = element;
- this.elements = {
- "items": this.element.find(".items"),
- "item": this.element.find(".item"),
- "inner": this.element.find(".inner"),
- "next": this.element.find(".next"),
- "prev": this.element.find(".prev")
- };
- this.options = _.extend(this.defaultOptions, options);
- this.scrollWidth = 0;
- this.slideWidth = 0;
- this.viewWidth = 0;
- this.init();
- };
- /**
- * Init
- */
- Scroll.prototype.init = function() {
- var that = this;
- //Slide and scroll width
- this.viewWidth = this.elements.inner.width();
- this.element.find(".item").each(function() {
- if(that.slideWidth === 0) {
- that.slideWidth = jQuery(this).outerWidth(true);
- }
- that.scrollWidth += parseInt(jQuery(this).outerWidth(true));
- });
- //Resize
- jQuery(window).resize(function(e) {
- that.resize();
- });
- that.resize();
- //Events
- this.events();
- };
- /**
- * Resize
- */
- Scroll.prototype.resize = function() {
- if(this.elements.items.width() < this.scrollWidth && this.elements.item.length > 1) {
- if(this.hasScroll !== true) {
- this.enableScroll();
- }
- } else {
- if(this.hasScroll !== false) {
- this.disableScroll();
- }
- }
- //slidesToShow
- var slidesToShow;
- this.viewWidth = this.elements.inner.width();
- if(this.hasScroll === true) {
- slidesToShow = Math.floor(this.viewWidth / this.slideWidth);
- if(slidesToShow<1) {
- slidesToShow = 1;
- }
- this.scroller.slickSetOption('slidesToShow', slidesToShow, true);
- }
- };
- /**
- * Enable scroll
- */
- Scroll.prototype.enableScroll = function() {
- this.hasScroll = true;
- this.element.addClass("scroll").removeClass("static");
- var slidesToShow = Math.floor(this.viewWidth / this.slideWidth);
- if(slidesToShow<1) {
- slidesToShow = 1;
- }
- this.options.slidesToShow = slidesToShow;
- this.elements.items.slick(this.options);
- this.scroller = this.elements.items.slick('getSlick');
- };
- /**
- * Disable scroll
- */
- Scroll.prototype.disableScroll = function() {
- this.hasScroll = false;
- this.element.removeClass("scroll").addClass("static");
- if(_.isObject(this.scroller)) {
- this.scroller.unslick();
- this.scroller = false;
- }
- };
- /**
- * Events
- */
- Scroll.prototype.events = function() {
- var that = this;
- //Next
- this.elements.next.bind('click.Scroll', function(e) {
- e.preventDefault();
- if(_.isObject(that.scroller)) {
- that.scroller.slickNext();
- }
- });
- //Prev
- this.elements.prev.bind('click.Scroll', function(e) {
- e.preventDefault();
- if(_.isObject(that.scroller)) {
- that.scroller.slickPrev();
- }
- });
- };
- window[namespace].Scroll = Scroll;
- })('edison');
Raw Paste