|
1 | 1 | // Original: https://github.com/Le-Stagiaire/jquery.cslider/blob/0c99322/src/jquery.cslider.js |
| 2 | +'use strict'; |
2 | 3 | (function ($) { |
3 | | - // Slider object |
4 | 4 | $.Slider = function (options, element) { |
5 | 5 | this.$el = $(element); |
6 | 6 | this._init(options); |
7 | 7 | }; |
8 | 8 | $.Slider.defaults = { |
9 | | - current: 0, // index of current slide |
10 | | - bgincrement: 100, // increment the bg position (parallax effect) when sliding |
11 | | - autoplay: true, // slideshow on / off |
12 | | - interval: 6000 // time between transitions |
| 9 | + current: 0, // Initial slide [index] |
| 10 | + bgincrement: 100, // Background parallax [pixels], set "0" to disable |
| 11 | + autoplay: true, // Slideshow enabled [true|false] |
| 12 | + interval: 6000 // Slideshow interval [milliseconds] |
13 | 13 | }; |
14 | 14 | $.Slider.prototype = { |
15 | 15 | _init: function (options) { |
16 | 16 | this.options = $.extend(true, {}, $.Slider.defaults, options); |
| 17 | + this.isAnimating = false; |
| 18 | + this.bgpositer = 0; |
| 19 | + // Detect slides |
17 | 20 | this.$slides = this.$el.find('div.da-slide'); |
18 | 21 | this.slidesCount = this.$slides.length; |
19 | | - this.current = this.options.current; |
20 | | - if (this.current < 0 || this.current >= this.slidesCount) { |
21 | | - this.current = 0; |
22 | | - } |
| 22 | + this.current = (this.options.current >= 0 && this.options.current < this.slidesCount) ? this.options.current : 0; |
23 | 23 | this.$slides.eq(this.current).addClass('da-slide-current'); |
| 24 | + // Create navigation dots |
24 | 25 | var $navigation = $('<nav class="da-dots"/>'); |
25 | 26 | for (var i = 0; i < this.slidesCount; ++i) { |
26 | | - $navigation.append('<span/>'); |
| 27 | + if (i === this.current) { |
| 28 | + $navigation.append('<span class="da-dots-current"/>'); |
| 29 | + } else { |
| 30 | + $navigation.append('<span/>'); |
| 31 | + } |
27 | 32 | } |
28 | 33 | $navigation.appendTo(this.$el); |
29 | 34 | this.$pages = this.$el.find('nav.da-dots > span'); |
| 35 | + // Detect navigation arrows |
30 | 36 | this.$navNext = this.$el.find('span.da-arrows-next'); |
31 | 37 | this.$navPrev = this.$el.find('span.da-arrows-prev'); |
32 | | - this.isAnimating = false; |
33 | | - this.bgpositer = 0; |
34 | | - |
35 | | - this._updatePage(); |
36 | | - // load the events |
| 38 | + // Register navigation events |
37 | 39 | this._loadEvents(); |
38 | | - // slideshow |
| 40 | + // Start slideshow if enabled |
39 | 41 | if (this.options.autoplay) { |
40 | 42 | this._startSlideshow(); |
41 | 43 | } |
42 | 44 | }, |
43 | 45 | _navigate: function (page, dir) { |
44 | | - var $current = this.$slides.eq(this.current); |
45 | 46 | if (this.current === page || this.isAnimating) { |
46 | 47 | return false; |
47 | 48 | } |
48 | 49 | this.isAnimating = true; |
49 | | - // check dir |
50 | | - var classTo, classFrom, d; |
51 | | - if (!dir) { |
52 | | - ( page > this.current ) ? d = 'next' : d = 'prev'; |
53 | | - } else { |
54 | | - d = dir; |
55 | | - } |
56 | | - if (d === 'next') { |
| 50 | + var $current = this.$slides.eq(this.current), |
| 51 | + $next = this.$slides.eq(page), |
| 52 | + rmClasses = 'da-slide-toleft da-slide-toright da-slide-fromleft da-slide-fromright', |
| 53 | + classTo, classFrom; |
| 54 | + // Direction |
| 55 | + if (dir === 'next') { |
57 | 56 | classTo = 'da-slide-toleft'; |
58 | 57 | classFrom = 'da-slide-fromright'; |
59 | 58 | ++this.bgpositer; |
|
62 | 61 | classFrom = 'da-slide-fromleft'; |
63 | 62 | --this.bgpositer; |
64 | 63 | } |
65 | | - this.$el.css('background-position-x', this.bgpositer * this.options.bgincrement + '%'); |
66 | | - this.current = page; |
67 | | - var $next = this.$slides.eq(this.current), |
68 | | - rmClasses = 'da-slide-toleft da-slide-toright da-slide-fromleft da-slide-fromright'; |
| 64 | + // Start background animation if enabled |
| 65 | + if (this.options.bgincrement) { |
| 66 | + this.$el.css('background-position-x', this.bgpositer * this.options.bgincrement + '%'); |
| 67 | + } |
| 68 | + // Start slide animation |
69 | 69 | $current.removeClass(rmClasses); |
70 | 70 | $next.removeClass(rmClasses); |
71 | 71 | $current.addClass(classTo); |
72 | 72 | $next.addClass(classFrom); |
73 | 73 | $current.removeClass('da-slide-current'); |
74 | 74 | $next.addClass('da-slide-current'); |
75 | | - this._updatePage(); |
76 | | - }, |
77 | | - _updatePage: function () { |
| 75 | + // Update navigation dots |
78 | 76 | this.$pages.removeClass('da-dots-current'); |
79 | | - this.$pages.eq(this.current).addClass('da-dots-current'); |
| 77 | + this.$pages.eq(page).addClass('da-dots-current'); |
| 78 | + this.current = page; |
80 | 79 | }, |
81 | 80 | _startSlideshow: function () { |
82 | 81 | var _self = this; |
83 | 82 | this.slideshow = setTimeout(function () { |
84 | | - var page = ( _self.current < _self.slidesCount - 1 ) ? page = _self.current + 1 : page = 0; |
| 83 | + var page = (_self.current < _self.slidesCount - 1) ? _self.current + 1 : 0; |
85 | 84 | _self._navigate(page, 'next'); |
86 | 85 | if (_self.options.autoplay) { |
87 | 86 | _self._startSlideshow(); |
|
94 | 93 | this.options.autoplay = false; |
95 | 94 | } |
96 | 95 | }, |
97 | | - page: function (idx) { |
98 | | - if (idx >= this.slidesCount || idx < 0) { |
99 | | - return false; |
100 | | - } |
101 | | - this._stopSlideshow(); |
102 | | - this._navigate(idx); |
103 | | - }, |
104 | 96 | _loadEvents: function () { |
105 | 97 | var _self = this; |
| 98 | + // Click on navigation dots |
106 | 99 | this.$pages.on('click.cslider', function () { |
107 | | - _self.page($(this).index()); |
| 100 | + _self._stopSlideshow(); |
| 101 | + var dir = ($(this).index() > _self.current) ? 'next' : 'prev'; |
| 102 | + _self._navigate($(this).index(), dir); |
108 | 103 | return false; |
109 | 104 | }); |
| 105 | + // Click on right navigation arrow |
110 | 106 | this.$navNext.on('click.cslider', function () { |
111 | 107 | _self._stopSlideshow(); |
112 | | - var page = ( _self.current < _self.slidesCount - 1 ) ? page = _self.current + 1 : page = 0; |
| 108 | + var page = (_self.current < _self.slidesCount - 1) ? _self.current + 1 : 0; |
113 | 109 | _self._navigate(page, 'next'); |
114 | 110 | return false; |
115 | 111 | }); |
| 112 | + // Click on left navigation arrow |
116 | 113 | this.$navPrev.on('click.cslider', function () { |
117 | 114 | _self._stopSlideshow(); |
118 | | - var page = ( _self.current > 0 ) ? page = _self.current - 1 : page = _self.slidesCount - 1; |
| 115 | + var page = (_self.current > 0) ? _self.current - 1 : _self.slidesCount - 1; |
119 | 116 | _self._navigate(page, 'prev'); |
120 | 117 | return false; |
121 | 118 | }); |
122 | | - if (!this.options.bgincrement) { |
123 | | - this.$el.on('animationend.cslider', function (event) { |
124 | | - if (_self.isAnimating && (event.originalEvent.animationName === 'fromRight' || event.originalEvent.animationName === 'fromLeft')) { |
| 119 | + // Flag to disable navigation during animation |
| 120 | + if (this.options.bgincrement) { |
| 121 | + this.$el.on('transitionend.cslider', function (event) { |
| 122 | + if (_self.isAnimating && (event.target === event.currentTarget)) { |
125 | 123 | _self.isAnimating = false; |
126 | 124 | } |
127 | 125 | }); |
128 | 126 | } else { |
129 | | - this.$el.on('transitionend.cslider', function (event) { |
130 | | - if (_self.isAnimating && (event.target.id === _self.$el.attr('id'))) { |
| 127 | + this.$el.on('animationend.cslider', function (event) { |
| 128 | + if (_self.isAnimating && (event.originalEvent.animationName === 'fromRight' || event.originalEvent.animationName === 'fromLeft')) { |
131 | 129 | _self.isAnimating = false; |
132 | 130 | } |
133 | 131 | }); |
134 | 132 | } |
135 | 133 | } |
136 | 134 | }; |
137 | | - var logError = function (message) { |
138 | | - if (this.console) { |
139 | | - console.error(message); |
140 | | - } |
141 | | - }; |
142 | 135 | $.fn.cslider = function (options) { |
143 | 136 | if (typeof options === 'string') { |
144 | 137 | var args = Array.prototype.slice.call(arguments, 1); |
145 | 138 | this.each(function () { |
146 | 139 | var instance = $.data(this, 'cslider'); |
147 | 140 | if (!instance) { |
148 | | - logError("cannot call methods on cslider prior to initialization; " + |
| 141 | + console.error("cannot call methods on cslider prior to initialization; " + |
149 | 142 | "attempted to call method '" + options + "'"); |
150 | 143 | } else if (!$.isFunction(instance[options]) || options.charAt(0) === "_") { |
151 | | - logError("no such method '" + options + "' for cslider instance"); |
| 144 | + console.error("no such method '" + options + "' for cslider instance"); |
152 | 145 | } else { |
153 | 146 | instance[options].apply(instance, args); |
154 | 147 | } |
|
0 commit comments