forked from pascalbajorat/simplefadeslideshow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fadeSlideShow.js
233 lines (202 loc) · 7.84 KB
/
fadeSlideShow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* fadeSlideShow
* v.2.2.0
*
* Copyright (c) 2010 Pascal Bajorat (http://www.pascal-bajorat.com)
* Dual licensed under the MIT (below)
* and GPL (http://www.gnu.org/licenses/gpl.txt) licenses.
*
*
* http://plugins.jquery.com/project/fadeslideshow
* http://www.pascal-bajorat.com
MIT License
Copyright (c) 2013 Pascal Bajorat
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
jQuery.fn.fadeSlideShow = function(options) {
return this.each(function(){
settings = jQuery.extend({
width: 640, // default width of the slideshow
height: 480, // default height of the slideshow
speed: 'slow', // default animation transition speed
interval: 3000, // default interval between image change
Active: 'fssActive', // default class for active stat
PlayPauseElement: 'fssPlayPause', // default css id for the play / pause element
PlayText: 'Play', // default play text
PauseText: 'Pause', // default pause text
NextElement: 'fssNext', // default id for next button
NextElementText: 'Next >', // default text for next button
PrevElement: 'fssPrev', // default id for prev button
PrevElementText: '< Prev', // default text for prev button
ListElement: 'fssList', // default id for image / content controll list
ListLi: 'fssLi', // default class for li's in the image / content controll
ListLiActive: 'fssActive', // default class for active state in the controll list
addListToId: false, // add the controll list to special id in your code - default false
allowKeyboardCtrl: true, // allow keyboard controlls left / right / space
autoplay: true // autoplay the slideshow
}, options);
// set style for wrapper element
jQuery(this).css({
width: settings.width,
height: settings.height,
position: 'relative',
overflow: 'hidden'
});
// set styles for child element
jQuery('> *',this).css({
position: 'absolute',
width: settings.width,
height: settings.height
});
// count number of slides
var Slides = jQuery('> *', this).length;
Slides = Slides - 1;
var ActSlide = Slides;
// Set jQuery Slide short var
var jQslide = jQuery('> *', this);
// save this
var fssThis = this;
var intval = false;
// Set active class to current active slide.
jQslide.removeClass(settings.Active);
jQslide.eq(ActSlide).addClass(settings.Active);
var autoplay = function(){
intval = setInterval(function(){
jQslide.eq(ActSlide).fadeOut(settings.speed);
// if list is on change the active class
if(settings.ListElement){
var setActLi = (Slides - ActSlide) + 1;
if(setActLi > Slides){setActLi=0;}
jQuery('#'+settings.ListElement+' li').removeClass(settings.ListLiActive);
jQuery('#'+settings.ListElement+' li').eq(setActLi).addClass(settings.ListLiActive);
}
if(ActSlide <= 0){
jQslide.fadeIn(settings.speed);
ActSlide = Slides;
}else{
ActSlide = ActSlide - 1;
}
// Set active class to current active slide.
jQslide.removeClass(settings.Active);
jQslide.eq(ActSlide).addClass(settings.Active);
}, settings.interval);
if(settings.PlayPauseElement){
jQuery('#'+settings.PlayPauseElement).html(settings.PauseText);
}
}
var stopAutoplay = function(){
clearInterval(intval);
intval = false;
if(settings.PlayPauseElement){
jQuery('#'+settings.PlayPauseElement).html(settings.PlayText);
}
}
var jumpTo = function(newIndex){
if(newIndex < 0){newIndex = Slides;}
else if(newIndex > Slides){newIndex = 0;}
if( newIndex >= ActSlide ){
jQuery('> *:lt('+(newIndex+1)+')', fssThis).fadeIn(settings.speed);
}else if(newIndex <= ActSlide){
jQuery('> *:gt('+newIndex+')', fssThis).fadeOut(settings.speed);
}
// set the active slide
ActSlide = newIndex;
// Set active class to current active slide.
jQslide.removeClass(settings.Active);
jQslide.eq(ActSlide).addClass(settings.Active);
if(settings.ListElement){
// set active
jQuery('#'+settings.ListElement+' li').removeClass(settings.ListLiActive);
jQuery('#'+settings.ListElement+' li').eq((Slides-newIndex)).addClass(settings.ListLiActive);
}
}
// if list is on render it
if(settings.ListElement){
var i=0;
var li = '';
while(i<=Slides){
if(i==0){
li = li+'<li class="'+settings.ListLi+i+' '+settings.ListLiActive+'"><a href="#">'+(i+1)+'<\/a><\/li>';
}else{
li = li+'<li class="'+settings.ListLi+i+'"><a href="#">'+(i+1)+'<\/a><\/li>';
}
i++;
}
var List = '<ul id="'+settings.ListElement+'">'+li+'<\/ul>';
// add list to a special id or append after the slideshow
if(settings.addListToId){
jQuery('#'+settings.addListToId).append(List);
}else{
jQuery(this).after(List);
}
jQuery('#'+settings.ListElement+' a').bind('click', function(){
var index = jQuery('#'+settings.ListElement+' a').index(this);
stopAutoplay();
var ReverseIndex = Slides-index;
jumpTo(ReverseIndex);
return false;
});
}
if(settings.PlayPauseElement){
if(!jQuery('#'+settings.PlayPauseElement).css('display')){
jQuery(this).after('<a href="#" id="'+settings.PlayPauseElement+'"><\/a>');
}
if(settings.autoplay){
jQuery('#'+settings.PlayPauseElement).html(settings.PauseText);
}else{
jQuery('#'+settings.PlayPauseElement).html(settings.PlayText);
}
jQuery('#'+settings.PlayPauseElement).bind('click', function(){
if(intval){
stopAutoplay();
}else{
autoplay();
}
return false;
});
}
if(settings.NextElement){
if(!jQuery('#'+settings.NextElement).css('display')){
jQuery(this).after('<a href="#" id="'+settings.NextElement+'">'+settings.NextElementText+'<\/a>');
}
jQuery('#'+settings.NextElement).bind('click', function(){
nextSlide = ActSlide-1;
stopAutoplay();
jumpTo(nextSlide);
return false;
});
}
if(settings.PrevElement){
if(!jQuery('#'+settings.PrevElement).css('display')){
jQuery(this).after('<a href="#" id="'+settings.PrevElement+'">'+settings.PrevElementText+'<\/a>');
}
jQuery('#'+settings.PrevElement).bind('click', function(){
prevSlide = ActSlide+1;
stopAutoplay();
jumpTo(prevSlide);
return false;
});
}
if(settings.allowKeyboardCtrl){
jQuery(document).bind('keydown', function(e){
if(e.which==39){
var nextSlide = ActSlide-1;
stopAutoplay();
jumpTo(nextSlide);
}else if(e.which==37){
var prevSlide = ActSlide+1;
stopAutoplay();
jumpTo(prevSlide);
}else if(e.which==32){
if(intval){stopAutoplay();}
else{autoplay();}
//return false;
}
});
}
// start autoplay or set it to false
if(settings.autoplay){autoplay();}else{intval=false;}
});
};