/
sequence.jquery.js
723 lines (644 loc) · 27.8 KB
/
sequence.jquery.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/*
Sequence.js (www.sequencejs.com)
Version: 0.2 Beta
Author: Ian Lunn @IanLunn
Author URL: http://www.ianlunn.co.uk/
Github: https://github.com/IanLunn/Sequence
This is a FREE script and is dual licensed under the following:
http://www.opensource.org/licenses/mit-license.php | http://www.gnu.org/licenses/gpl.html
Sequence.js and its dependencies are (c) Ian Lunn Design 2012 unless otherwise stated.
Aside from these comments, you may modify and distribute this file as you please. Have fun!
*/
/* Modernizr 2.0.6 (Custom Build) | MIT & BSD
* Build: http://www.modernizr.com/download/#-prefixed-testprop-testallprops-domprefixes
*/
;window.Modernizr=function(a,b,c){function z(a,b){var c=a.charAt(0).toUpperCase()+a.substr(1),d=(a+" "+m.join(c+" ")+c).split(" ");return y(d,b)}function y(a,b){for(var d in a)if(j[a[d]]!==c)return b=="pfx"?a[d]:!0;return!1}function x(a,b){return!!~(""+a).indexOf(b)}function w(a,b){return typeof a===b}function v(a,b){return u(prefixes.join(a+";")+(b||""))}function u(a){j.cssText=a}var d="2.0.6",e={},f=b.documentElement,g=b.head||b.getElementsByTagName("head")[0],h="modernizr",i=b.createElement(h),j=i.style,k,l=Object.prototype.toString,m="Webkit Moz O ms Khtml".split(" "),n={},o={},p={},q=[],r,s={}.hasOwnProperty,t;!w(s,c)&&!w(s.call,c)?t=function(a,b){return s.call(a,b)}:t=function(a,b){return b in a&&w(a.constructor.prototype[b],c)};for(var A in n)t(n,A)&&(r=A.toLowerCase(),e[r]=n[A](),q.push((e[r]?"":"no-")+r));u(""),i=k=null,e._version=d,e._domPrefixes=m,e.testProp=function(a){return y([a])},e.testAllProps=z,e.prefixed=function(a){return z(a,"pfx")};return e}(this,this.document);
(function($){
function Sequence(element, options){
//GLOBAL PARAMETERS
this.container = $(element);
this.sequence = this.container.children("ul");
var self = this,
prefixes = {
'WebkitTransition' : '-webkit-',
'MozTransition' : '-moz-',
'OTransition' : '-o-',
'msTransition' : '-ms-',
'transition' : ''
},
transitions = {
'WebkitTransition' : 'webkitTransitionEnd webkitAnimationEnd',
'MozTransition' : 'transitionend animationend',
'OTransition' : 'oTransitionEnd oAnimationEnd',
'msTransition' : 'MSTransitionEnd MSAnimationEnd',
'transition' : 'transitionend animationend'
};
self.prefix = prefixes[Modernizr.prefixed('transition')],
self.transitionEnd = transitions[Modernizr.prefixed('transition')],
self.transitionProperties = {},
self.numberOfFrames = self.sequence.children("li").length,
self.transitionsSupported = (self.prefix != undefined) ? true : false, //determine if transitions are supported
self.hasTouch = ("ontouchstart" in window) ? true : false, //determine if this is a touch enabled device
self.sequenceTimer,
self.paused = false,
self.hoverEvent,
self.defaultPreloader;
self.init = {
preloader: function(optionPreloader){
prependPreloaderTo = (self.settings.prependPreloader == true) ? self.container : self.settings.prependPreloader;
opacity = (self.transitionsSupported) ? 0 : 1;
switch(optionPreloader){
case true:
case undefined:
//append the default preloader styles
$("head").append("<style>#sequence-preloader{height: 100%;position: absolute;width: 100%;z-index: 999999;}@"+self.prefix+"keyframes preload{0%{opacity: 0;}50%{opacity: 1;}100%{opacity: 0;}}@keyframes preload{0%{opacity: 0;}50%{opacity: 1;}100%{opacity: 0;}}#sequence-preloader img{background: #ff9933;border-radius: 6px;display: inline-block;height: 12px;opacity: "+opacity+";position: relative;top: -50%;width: 12px;"+self.prefix+"animation: preload 1s infinite; animation: preload 1s infinite;}.preloading{height: 12px;margin: 0 auto;top: 50%;position: relative;width: 48px;}#sequence-preloader img:nth-child(2){"+self.prefix+"animation-delay: .15s; animation-delay: .15s;}#sequence-preloader img:nth-child(3){"+self.prefix+"animation-delay: .3s; animation-delay: .3s;}.preloading-complete{opacity: 0;visibility: hidden;"+self.prefix+"transition-duration: 1s; transition-duration: 1s;}</style>");
$(prependPreloaderTo).prepend('<div id="sequence-preloader"><div class="preloading"><img src="../../images/sequence-preloader.png" alt="Sequence is loading, please wait..." /> <img src="../../images/sequence-preloader.png" alt="Sequence is loading, please wait..." /> <img src="../../images/sequence-preloader.png" alt="Sequence is loading, please wait..." /></div></div>');
if(!self.transitionsSupported || self.prefix == "-o-"){
self.preloaderFallback();
}
return $("#sequence-preloader");
break;
case false:
break;
default:
this.CSSSelectorToHTML($(prependPreloaderTo), optionPreloader);
return $(optionPreloader);
break;
}
},
navButtons: function(optionButton, direction){
prependNextButtonTo = (self.settings.prependNextButton == true) ? self.container : self.settings.prependNextButton;
prependPrevButtonTo = (self.settings.prependPrevButton == true) ? self.container : self.settings.prependPrevButton;
switch(optionButton){
case true: //set up default nav button
case undefined:
if(direction == ".next"){
this.CSSSelectorToHTML($(prependNextButtonTo), $.fn.sequence.defaults.nextButton);
}else{
this.CSSSelectorToHTML($(prependPrevButtonTo), $.fn.sequence.defaults.prevButton);
}
break;
case false: //don't use a nav button
break;
default: //set up the dev defined button
if(direction == ".next"){
this.CSSSelectorToHTML($(prependNextButtonTo), optionButton);
$(optionButton).show(); //this will most likely be removed in future versions
return $(optionButton);
}else{
this.CSSSelectorToHTML($(prependPrevButtonTo), optionButton);
$(optionButton).show(); //this will most likely be removed in future versions
return $(optionButton);
}
break;
}
},
pauseIcon: function(pauseIcon, pauseIconSrc){
prependPauseIconTo = (self.settings.prependPauseIcon == true) ? self.container : self.settings.prependPauseIcon;
switch(pauseIcon){
case true:
case undefined:
this.CSSSelectorToHTML($(prependPauseIconTo), $.fn.sequence.defaults.pauseIcon, pauseIconSrc);
$($.fn.sequence.defaults.pauseIcon).hide();
return $($.fn.sequence.defaults.pauseIcon);
break;
case false:
break;
default:
this.CSSSelectorToHTML($(prependPauseIconTo), this.settings.pauseIcon, pauseIconSrc);
$(this.settings.pauseIcon).hide();
return $(this.settings.pauseIcon);
break;
}
},
CSSSelectorToHTML: function(prependTo, selector, pauseIconSrc){
switch(selector.charAt(0)){
case ".":
buttonSelector = 'class="'+selector.split(".")[1]+'"';
break;
case "#":
buttonSelector = 'id="'+selector.split("#")[1]+'"';
break;
default:
buttonSelector = selector;
break;
}
if(pauseIconSrc != undefined && (self.settings.pauseIcon != true || self.settings.pauseIcon != undefined)){
$(prependTo).prepend('<div '+buttonSelector+'><img src="'+pauseIconSrc+'" alt="Pause" /></div>');
}else{
$(prependTo).prepend('<div '+buttonSelector+'></div>');
}
}
},
//INIT
self.settings = $.extend({}, $.fn.sequence.defaults, options);
self.settings.preloader = self.init.preloader(self.settings.preloader);
if(self.settings.animateStartingFrameIn){
self.modifyElements(self.sequence.children("li").children(), "0s");
self.sequence.children("li").children().removeClass("animate-in");
}
if(options.preloader != false){
$(window).bind("load", function(){
self.settings.afterPreload();
if(self.settings.hidePreloaderUsingCSS && self.transitionsSupported && self.prefix != "-o-"){
prependPreloadingCompleteTo = (self.settings.prependPreloadingComplete == true) ? self.settings.preloader : $(self.settings.prependPreloadingComplete);
prependPreloadingCompleteTo.addClass("preloading-complete");
setTimeout(init, self.settings.hidePreloaderDelay);
}else{
self.settings.preloader.fadeOut(self.settings.hidePreloaderDelay, function(){
clearInterval(self.defaultPreloader);
init();
});
}
$(window).unbind("load");
});
}else{
init();
}
function init(){
self.settings.nextButton = self.init.navButtons(options.nextButton, $.fn.sequence.defaults.nextButton);
self.settings.prevButton = self.init.navButtons(options.prevButton, $.fn.sequence.defaults.prevButton);
self.settings.prependPauseIcon = (self.settings.prependPauseIcon != undefined)
? self.settings.prependPauseIcon
: self.container;
if(self.hasTouch){
self.settings.calculatedSwipeThreshold = self.container.width() * (self.settings.swipeThreshold / 100);
}
self.settings.pauseIcon = self.init.pauseIcon(self.settings.pauseIcon, self.settings.pauseIconSrc);
self.currentFrame = self.sequence.children("li:nth-child("+self.settings.startingFrameID+")").addClass("current");
self.currentFrameChildren = self.currentFrame.children();
self.currentFrameID = self.settings.startingFrameID;
self.nextFrameID = self.currentFrameID;
self.sequence.children("li").children().removeClass("animate-in");
self.direction;
self.sequence.css({"width": "100%", "height": "100%"}); //set the sequence list to 100% width/height just incase it hasn't been specified in the CSS
if(self.transitionsSupported){ //initiate the full featured Sequence if transitions are supported...
whenFirstAnimateInEnds = function(){
animationComplete = function(){
self.settings.afterNextFrameAnimatesIn();
self.active = false;
if(self.settings.autoPlay){
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
}
}
self.waitForAnimationsToComplete(self.currentFrameChildren, animationComplete);
}
if(!self.settings.animateStartingFrameIn){ //start first frame in animated in position
self.modifyElements(self.currentFrameChildren, "0s");
self.currentFrameChildren.addClass("animate-in");
setTimeout(function(){
self.modifyElements(self.currentFrameChildren, "");
}, 100);
if(self.settings.autoPlay){
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
}
}else if(self.settings.reverseAnimationsWhenNavigatingBackwards && self.settings.autoPlayDirection -1 && self.settings.animateStartingFrameIn){
self.active = true;
self.modifyElements(self.currentFrameChildren, "0s");
self.currentFrameChildren.addClass("animate-out");
self.settings.beforeNextFrameAnimatesIn();
setTimeout(function(){
self.modifyElements(self.currentFrameChildren, "");
self.currentFrameChildren.removeClass("animate-out");
self.currentFrameChildren.addClass("animate-in");
}, 100);
whenFirstAnimateInEnds();
}else{
self.active = true;
self.settings.beforeCurrentFrameAnimatesIn();
setTimeout(function(){
self.modifyElements(self.currentFrameChildren, "");
self.currentFrameChildren.addClass("animate-in");
}, 100);
whenFirstAnimateInEnds();
}
}else{ //initiate a basic slider for browsers that don't support CSS3 transitions
self.sequence.children("li").children().css("opacity", "0").addClass("animate-in").animate({"opacity": "1"}, 500);
self.currentFrame.css("z-index", self.numberOfFrames);
self.sequence.children(":not(li:nth-child("+self.settings.startingFrameID+"))").css({"display": "none", "opacity": 0});
if(self.settings.autoPlay){
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
}
}
//END INIT
//EVENTS
if(self.settings.nextButton != undefined){
self.settings.nextButton.click(function(){
self.next();
});
}
if(self.settings.prevButton != undefined){
self.settings.prevButton.click(function(){
self.prev();
});
}
if(self.settings.keysNavigate == true){
$(document).keydown(function(e){
if(e.keyCode == 39){
self.next();
}
if(e.keyCode == 37){
self.prev();
}
});
}
if(self.settings.pauseOnHover && self.settings.autoPlay){
self.hoverEvent = self.sequence.hover(function(e){
/*offsetWidth = e.target.offsetWidth; //width of the element
offsetX = e.offsetX; //cursor position on the element (or left pos (?))
//offsetLeft = e.target.offsetLeft; //left position from the container
console.log(e);
//cursorPos = offsetLeft + offsetX;
//console.log(cursorPos);
if(e.target.offsetLeft >= 0 && offsetX <= self.sequence.width()){
console.log("same");
}*/
self.settings.autoPlay = false;
clearTimeout(self.sequenceTimer);
$(self.settings.pauseIcon).show();
}, function(){
self.settings.autoPlay = true;
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
$(self.settings.pauseIcon).hide();
});
}
if(self.settings.touchEnabled && self.hasTouch){
var touch,
touches = {
"touchstart": -1,
"touchmove" : -1,
"swipeDirection" : ""
};
self.sequence.bind("touchstart touchmove touchend", function(e){
e.preventDefault();
switch(e.originalEvent.type){
case "touchstart":
case "touchmove":
touches[e.originalEvent.type] = e.originalEvent.touches[0].pageX;
break;
case 'touchend':
if(touches["touchstart"] > -1 && touches["touchmove"] > self.settings.calculatedSwipeThreshold){
if(touches["touchstart"] < touches["touchmove"]){
self.next();
}else{
self.prev();
}
}
default:
break;
}
});
}
$(window).resize(function(){ //if the window is resized...
self.settings.calculatedSwipeThreshold = self.container.width() * (self.settings.swipeThreshold / 100); //recalculate the swipe threshold
});
//END EVENTS
}
} //END CONSTRUCTOR
Sequence.prototype = {
preloaderFallback: function(){ //if transitions aren't supported, call this fallback to show the default preloading animations
i = 0;
function preload(){
i = (i == 1) ? 0 : 1;
$("#sequence-preloader img:nth-child(1)").animate({"opacity": i}, 100);
$("#sequence-preloader img:nth-child(2)").animate({"opacity": i}, 350);
$("#sequence-preloader img:nth-child(3)").animate({"opacity": i}, 600);
}
preload();
self.defaultPreloader = setInterval(function(){
preload();
}, 600);
},
autoPlaySequence: function(direction){
var self = this;
if(self.settings.autoPlayDirection == 1){
self.next();
}else{
self.prev();
}
},
modifyElements: function($elementToReset, cssValue){
var self = this;
$elementToReset.css(
self.prefixCSS(self.prefix, {
"transition-duration": cssValue,
"transition-delay": cssValue
})
)
},
//adds the browser vendors prefix onto multiple CSS properties
prefixCSS: function(prefix, properties){
css = {};
for(property in properties){
css[prefix + property] = properties[property];
}
return css;
},
//Opera workaround: currently Opera has a bug that prevents retrieving a prefixed CSS property from the DOM, meaning we have to search through the CSS file instead. It's not ideal in terms of performance but hopefully it'll be fixed in the future
getStyleBySelector: function(selector){
css = {};
var sheetList = document.styleSheets, ruleList, i, j;
for(i = sheetList.length - 1; i >= 0; i--){
error = false;
try{
ruleList = sheetList[i].cssRules;
}
catch(e){
error = true;
}
if(!error){
for(j = 0; j < ruleList.length; j++){
if(ruleList[j].type == CSSRule.STYLE_RULE && ruleList[j].selectorText == selector){
css["-o-transition-duration"] = ruleList[j].style.OTransitionDuration;
css["-o-transition-delay"] = ruleList[j].style.OTransitionDelay;
return css;
}
}
}
}
return null;
},
startAutoPlay: function(wait, newAutoPlayDelay){
var self = this;
wait = (wait == undefined) ? 0 : wait;
self.settings.autoPlayDelay = (newAutoPlayDelay == undefined) ? self.settings.autoPlayDelay : newAutoPlayDelay;
self.settings.autoPlay = true;
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
if(self.settings.pauseOnHover){
self.hoverEvent = self.sequence.hover(function(){
self.settings.autoPlay = false;
clearTimeout(self.sequenceTimer);
$(self.settings.pauseIcon).show();
}, function(){
self.settings.autoPlay = true;
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
$(self.settings.pauseIcon).hide();
});
}
},
stopAutoPlay: function(){
var self = this;
self.settings.autoPlay = false;
clearTimeout(self.sequenceTimer);
if(self.hoverEvent != undefined){
self.hoverEvent.unbind();
}
},
next: function(){
var self = this;
if(!self.active){
if(self.settings.cycle || (!self.settings.cycle && self.currentFrame.index() + 1 != self.numberOfFrames)){
if(self.paused){
self.paused = false;
self.startAutoPlay();
}
self.nextFrameID = (self.currentFrame.index() + 1 != self.numberOfFrames) ? self.currentFrameID + 1 : 1;
self.goTo(self.nextFrameID, 1); //go to the next frame
}else if(self.settings.autoPlayDirection == 1){
self.paused = true;
self.stopAutoPlay();
}
}
},
prev: function(){
var self = this;
if(!self.active){
if(self.settings.cycle || (!self.settings.cycle && self.currentFrame.index() + 1 != 1)){
if(self.paused){
self.paused = false;
self.startAutoPlay();
}
self.nextFrameID = (self.currentFrame.index() + 1 == 1) ? self.numberOfFrames : self.currentFrameID - 1;
self.goTo(self.nextFrameID, -1); //go to the prev frame
}else if(self.settings.autoPlayDirection == -1){
self.paused = true;
self.stopAutoPlay();
}
}
},
goTo: function(id, direction){ //where the magic happens
var self = this;
if(id == self.numberOfFrames){
self.settings.beforeLastFrameAnimatesIn();
}else if(id == 1){
self.settings.beforeFirstFrameAnimatesIn();
}
if(id == self.currentFrame.index() + 1){
return false;
}else if(!self.active){ //if there are no animations running...
self.active = true; //set the sequence as active
self.currentFrame = self.sequence.children(".current"); //find which frame is active
nextFrame = self.sequence.children("li:nth-child("+id+")"); //grab the next frame
if(direction == undefined){ //if no direction is specified...
self.direction = (id > self.currentFrameID) ? 1 : -1; //work out which way to go based on what frame is currently active
}else{
self.direction = direction;
}
frameChildren = self.currentFrame.children(); //save the child elements
nextFrameChildren = nextFrame.children(); //save the child elements
if(self.transitionsSupported){ //if the browser supports CSS3 transitions...
self.settings.beforeCurrentFrameAnimatesOut();
self.animateOut(self.direction);
animateIn = function(){
self.animateIn(self.direction);
self.currentFrameID = id;
}
switch(self.settings.delayDuringOutInTransitions){
case true:
self.waitForAnimationsToComplete(frameChildren, animateIn);
break;
case false:
animateIn();
break;
default:
setTimeout(animateIn, self.settings.delayDuringOutInTransitions);
break;
}
}else{ //if the browser doesn't support CSS3 transitions...
self.sequence.children("li").css({"position": "relative"}); //this allows for fadein/out in IE
self.currentFrame.animate({"opacity": 0}, self.settings.fallbackTheme.speed, function(){ //hide the current frame
self.currentFrame.css({"display": "none", "z-index": "1"});
self.currentFrame.removeClass("current");
self.settings.beforeNextFrameAnimatesIn();
nextFrame.addClass("current").css({"display": "block", "z-index": self.numberOfFrames}).animate({"opacity": 1}, 500); //make the next frame the current one and show it
self.currentFrame = nextFrame;
self.currentFrameID = self.currentFrame.index() + 1;
self.active = false;
if(self.settings.autoPlay){
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
}
});
}
}
},
animateOut: function(direction){
var self = this;
self.settings.beforeCurrentFrameAnimatesIn();
if(!self.settings.reverseAnimationsWhenNavigatingBackwards || direction == 1){ //if user hit next button...
//reset the position of the next frames elements ready for animating in again
self.modifyElements(nextFrameChildren, "0s");
nextFrameChildren.removeClass("animate-out");
//make the current frames elements animate out
self.modifyElements(frameChildren, "");
if(!self.settings.disableAnimateOut){
frameChildren.addClass("animate-out").removeClass("animate-in");
}
}
if(self.settings.reverseAnimationsWhenNavigatingBackwards && direction == -1){ //if the user hit prev button
self.modifyElements(nextFrameChildren, "0s");
if(!self.settings.disableAnimateOut){
nextFrameChildren.addClass("animate-out");
}else{
self.active = false;
}
self.modifyElements(frameChildren, "");
frameChildren.each(function(){
if(self.prefix == "-o-"){
selector = "." + $(this).attr("class").replace(" ", ".");
previousFrameTransitionProperties = self.getStyleBySelector(selector);
self.transitionProperties["transition-duration"] = previousFrameTransitionProperties["-o-transition-duration"];
self.transitionProperties["transition-delay"] = previousFrameTransitionProperties["-o-transition-delay"];
self.transitionProperties["transition-delay"] = (self.transitionProperties["transition-delay"] == "") ? "0s" : self.transitionProperties["transition-delay"];
}else{
self.transitionProperties["transition-duration"] = $(this).css(self.prefix + "transition-duration");
self.transitionProperties["transition-delay"] = $(this).css(self.prefix + "transition-delay");
}
$(this).css(
self.prefixCSS(self.prefix, self.transitionProperties)
).removeClass("animate-in");
})
}
},
waitForAnimationsToComplete: function(elements, onceComplete){
var self = this;
elementsAnimated = {};
elements.each(function(){
elementsAnimated[$(this).attr("class")] = false;
});
self.currentFrame.bind(self.transitionEnd, function(e){ //wait for elements to finish animating...
elementsAnimated[e.target.className] = true;
total = 0;
for(element in elementsAnimated){
if(elementsAnimated[element] == true){
total++;
}
}
if(total == elements.length){
onceComplete();
}
});
},
animateIn: function(direction){
var self = this;
self.currentFrame.removeClass("current"); //remove the active class
self.currentFrame.unbind(self.transitionEnd); //remove the animation end event
nextFrame.addClass("current"); //activate the next frame
self.currentFrame = nextFrame; //the next frame is now the current one
if(direction == 1){
self.currentFrameID = (self.currentFrameID != self.numberOfFrames) ? self.currentFrameID + 1 : 1;
}else{
self.currentFrameID = (self.currentFrameID != 1) ? self.currentFrameID - 1 : self.numberOfFrames;
}
nextFrameChildren = nextFrame.children(); //save the child elements
frameChildren = self.currentFrame.children(); //save the child elements (the ones we'll animate) in an array
self.settings.beforeNextFrameAnimatesIn();
if(!self.settings.reverseAnimationsWhenNavigatingBackwards || direction == 1){ //if user hit next button...
//reset the position of the next frames elements ready for animating in again
self.modifyElements(nextFrameChildren, "0s");
nextFrameChildren.removeClass("animate-out");
setTimeout(function(){
frameChildren.removeClass("animate-out");
self.modifyElements(frameChildren, "");
frameChildren.addClass("animate-in");
whenAnimationEnds();
}, 50);
}
if(self.settings.reverseAnimationsWhenNavigatingBackwards && direction == -1){ //if the user hit prev button
setTimeout(function(){
self.modifyElements(frameChildren, "");
frameChildren.addClass("animate-in").removeClass("animate-out");
whenAnimationEnds();
}, 50);
}
whenAnimationEnds = function(){
unbind = function(){
self.settings.afterNextFrameAnimatesIn();
if(self.currentFrameID == self.numberOfFrames){
self.settings.afterLastFrameAnimatesIn();
}else if(self.currentFrameID == 1){
self.settings.afterFirstFrameAnimatesIn();
}
self.currentFrame.unbind(self.transitionEnd); //unbind waiting for the frame to finish as it's no longer needed
self.active = false; //set the sequence as inactive to allow the next frames to be activated
if(self.settings.autoPlay){
autoPlaySequence = function(){self.autoPlaySequence()};
clearTimeout(self.sequenceTimer);
self.sequenceTimer = setTimeout(autoPlaySequence, self.settings.autoPlayDelay, self);
}
}
self.waitForAnimationsToComplete(nextFrameChildren, unbind);
}
}
}; //END PROTOTYPE
$.fn.sequence = function(options){
return this.each(function(){
var sequence = new Sequence($(this), options);
$(this).data("sequence", sequence);
});
};
$.fn.sequence.defaults = {
nextButton: ".next",
prependNextButton: true,
prevButton: ".prev",
prependPrevButton: true,
preloader: true,
prependPreloader: true,
prependPreloadingComplete: true,
hidePreloaderUsingCSS: true,
hidePreloaderDelay: 0,
startingFrameID: 1,
autoPlay: true,
autoPlayDirection: 1,
animatestartingFrameIn: false,
autoPlayDelay: 5000,
pauseOnHover: true,
pauseIcon: false,
prependPauseIcon: true,
pauseIconSrc: "images/pause-icon.png",
keysNavigate: true,
delayDuringOutInTransitions: 1000,
touchEnabled: true,
swipeThreshold: 15,
cycle: true,
disableAnimateOut: false,
reverseAnimationsWhenNavigatingBackwards: true,
fallbackTheme: {
speed: 500
},
//Callbacks are a work in progress and note it's possible not all of them will make it into v1.0
beforeCurrentFrameAnimatesOut: function(){}, //triggers before current frame begins to animate out
beforeNextFrameAnimatesIn: function(){}, //triggers before next frame begins to animate in
afterNextFrameAnimatesIn: function(){}, //triggers after next frame animates in (and becomes the current frame)
beforeCurrentFrameAnimatesIn: function(){},
beforeFirstFrameAnimatesIn: function(){}, //triggers before the first frame animates in
afterFirstFrameAnimatesIn: function(){}, //triggers after the first frame animates in
beforeLastFrameAnimatesIn: function(){}, //triggers before the last frame animates in
afterLastFrameAnimatesIn: function(){}, //triggers after the last frame animates in
afterPreload: function(){}
};
$.fn.sequence.settings = {};
})(jQuery);