/
sequence.js
3957 lines (3078 loc) · 126 KB
/
sequence.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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Sequence.js
*
* The responsive CSS animation framework for creating unique sliders,
* presentations, banners, and other step-based applications.
*
* @link https://github.com/IanLunn/Sequence
* @author IanLunn
* @version 2.1.0
* @license http://sequencejs.com/licenses/
* @copyright Ian Lunn Design Limited 2015
*/
function defineSequence(imagesLoaded, Hammer) {
'use strict';
var instances = [],
instance = 0;
/**
* Sequence function
*
* @param {Object} element - the element Sequence is bound to
* @param {Object} options - this instance's options
* @returns {Object} self - Properties and methods available to this instance
*/
var Sequence = (function (element, options) {
var instanceId = element.getAttribute("data-seq-enabled");
// Prevent multiple instances on the same element. Return the object instead
if (instanceId !== null) {
return instances[instanceId];
}
// The element now has Sequence attached to it
element.setAttribute("data-seq-enabled", instance);
instance++;
/* --- PRIVATE VARIABLES/FUNCTIONS --- */
// Default Sequence settings
var defaults = {
/* --- General --- */
// The first step to show
startingStepId: 1,
// Should the starting step animate in to begin with?
startingStepAnimatesIn: false,
// When the last step is reached, should Sequence cycle back to the start?
cycle: true,
// How long to wait between the current phase animating out, and the next
// animating in.
phaseThreshold: true,
// Should transitions be reversed when navigating backwards?
reverseWhenNavigatingBackwards: false,
// Should transition-timing-function be reversed when navigating backwards?
reverseTimingFunctionWhenNavigatingBackwards: false,
// Should the active step be given a higher z-index?
moveActiveStepToTop: true,
/* --- Canvas Animation --- */
// Should the canvas automatically animate between steps?
animateCanvas: true,
// Time it should take to animate between steps
animateCanvasDuration: 250,
/* --- autoPlay --- */
// Cause Sequence to automatically navigate between steps
// Specify a number in milliseconds or true (for a default of 5000ms) to
// define the period Sequence should wait between each step before
// navigating to the next step
autoPlay: false,
// How long to wait between each step before navigation occurs again
autoPlayInterval: 5000,
// Amount of time to wait until autoPlay starts again after being stopped
autoPlayDelay: null,
// Direction of navigation when autoPlay is enabled
autoPlayDirection: 1,
// Use an autoPlay button? You can also specify a CSS selector to
// change what element acts as the button. If true, the element uses the
// class of "seq-autoplay"
autoPlayButton: true,
// Pause autoPlay when the Sequence element is hovered over
autoPlayPauseOnHover: true,
/* --- Navigation Skipping --- */
// Allow the user to navigate between steps even if they haven't
// finished animating
navigationSkip: true,
// How long to wait before the user is allowed to skip to another step
navigationSkipThreshold: 250,
// Fade a step when it has been skipped
fadeStepWhenSkipped: true,
// How long the fade should take
fadeStepTime: 500,
// When a step is skipped, the next step will immediately animate-in
// regardless of the phaseThreshold option
ignorePhaseThresholdWhenSkipped: false,
// Don't allow the user to go to a previous step when the current one is
// still active
preventReverseSkipping: false,
/* --- Next/Prev Button --- */
// Use next and previous buttons? You can also specify a CSS selector to
// change what element acts as the button. If true, the element uses
// classes of "seq-next" and "seq-prev"
nextButton: true,
prevButton: true,
/* --- Pagination --- */
// Use pagination? You can also specify a CSS selector to
// change what element acts as pagination. If true, the element uses the
// class of "seq-pagination"
pagination: true,
/* --- Preloader --- */
// You can also specify a CSS selector to
// change what element acts as the preloader. If true, the element uses
// the class of "seq-preloader"
preloader: false,
// Preload all images from specific steps
preloadTheseSteps: [1],
// Preload specified images
preloadTheseImages: [
/**
* Example usage
* "images/catEatingSalad.jpg",
* "images/grandmaDressedAsBatman.png"
*/
],
// Hide Sequence's steps until it has preloaded
hideStepsUntilPreloaded: false,
// (Debugging only) Prevent the preloader from hiding so you can
// test it's styles
pausePreloader: false,
/* --- Keyboard --- */
// Can the user navigate between steps by pressing keyboard buttons?
keyNavigation: false,
// When numeric keys 1 - 9 are pressed, Sequence will navigate to the
// corresponding step
numericKeysGoToSteps: false,
// Events to run when the user presses the left/right keys
keyEvents: {
left: function(sequence) {sequence.prev();},
right: function(sequence) {sequence.next();}
},
/* --- Touch Swipe --- */
// Can the user navigate between steps by swiping on a touch enabled device?
swipeNavigation: true,
// Events to run when the user swipes in a particular direction
swipeEvents: {
left: function(sequence) {sequence.next();},
right: function(sequence) {sequence.prev();},
up: undefined,
down: undefined
},
// Options to supply the third-party Hammer library See: http://hammerjs.github.io/recognizer-swipe/
swipeHammerOptions: {},
/* --- hashTags --- */
// Should the URL update to include a hashTag that relates to the current
// step being shown?
hashTags: false,
// Get the hashTag from an ID or data-seq-hashtag attribute?
hashDataAttribute: false,
// Should the hash change on the first step?
hashChangesOnFirstStep: false,
/* --- Fallback Theme --- */
// Settings to use when the browser doesn't support CSS transitions
fallback: {
// The speed to transition between steps
speed: 500
}
};
// Default value for autoPlay in milliseconds
var autoPlayDefault = 5000;
// See sequence.animation.domDelay() for an explanation of this
var domThreshold = 50;
// Throttle the window resize event
// see self.manageEvents.add.resizeThrottle()
var resizeThreshold = 100;
// Convert browser fixes to CSS strings
var prefixTranslations = {
animation: {
"WebkitAnimation": "-webkit-",
"animation": ""
}
};
/**
*
* This version of Modernizr is for use with Sequence.js and is included
* internally to prevent conflicts with other Modernizr builds.
*
* Modernizr 2.8.3 (Custom Build) | MIT & BSD
* Build: http://modernizr.com/download/#-cssanimations-csstransforms-csstransitions-svg-touch-prefixed-teststyles-testprop-testallprops-prefixes-domprefixes
*/
/* jshint ignore:start */
var Modernizr=function(a,b,c){function z(a){i.cssText=a}function A(a,b){return z(l.join(a+";")+(b||""))}function B(a,b){return typeof a===b}function C(a,b){return!!~(""+a).indexOf(b)}function D(a,b){for(var d in a){var e=a[d];if(!C(e,"-")&&i[e]!==c)return b=="pfx"?e:!0}return!1}function E(a,b,d){for(var e in a){var f=b[a[e]];if(f!==c)return d===!1?a[e]:B(f,"function")?f.bind(d||b):f}return!1}function F(a,b,c){var d=a.charAt(0).toUpperCase()+a.slice(1),e=(a+" "+n.join(d+" ")+d).split(" ");return B(b,"string")||B(b,"undefined")?D(e,b):(e=(a+" "+o.join(d+" ")+d).split(" "),E(e,b,c))}var d="2.8.3",e={},f=b.documentElement,g="modernizr",h=b.createElement(g),i=h.style,j,k={}.toString,l=" -webkit- -moz- -o- -ms- ".split(" "),m="Webkit Moz O ms",n=m.split(" "),o=m.toLowerCase().split(" "),p={svg:"http://www.w3.org/2000/svg"},q={},r={},s={},t=[],u=t.slice,v,w=function(a,c,d,e){var h,i,j,k,l=b.createElement("div"),m=b.body,n=m||b.createElement("body");if(parseInt(d,10))while(d--)j=b.createElement("div"),j.id=e?e[d]:g+(d+1),l.appendChild(j);return h=["­",'<style id="s',g,'">',a,"</style>"].join(""),l.id=g,(m?l:n).innerHTML+=h,n.appendChild(l),m||(n.style.background="",n.style.overflow="hidden",k=f.style.overflow,f.style.overflow="hidden",f.appendChild(n)),i=c(l,a),m?l.parentNode.removeChild(l):(n.parentNode.removeChild(n),f.style.overflow=k),!!i},x={}.hasOwnProperty,y;!B(x,"undefined")&&!B(x.call,"undefined")?y=function(a,b){return x.call(a,b)}:y=function(a,b){return b in a&&B(a.constructor.prototype[b],"undefined")},Function.prototype.bind||(Function.prototype.bind=function(b){var c=this;if(typeof c!="function")throw new TypeError;var d=u.call(arguments,1),e=function(){if(this instanceof e){var a=function(){};a.prototype=c.prototype;var f=new a,g=c.apply(f,d.concat(u.call(arguments)));return Object(g)===g?g:f}return c.apply(b,d.concat(u.call(arguments)))};return e}),q.touch=function(){var c;return"ontouchstart"in a||a.DocumentTouch&&b instanceof DocumentTouch?c=!0:w(["@media (",l.join("touch-enabled),("),g,")","{#modernizr{top:9px;position:absolute}}"].join(""),function(a){c=a.offsetTop===9}),c},q.cssanimations=function(){return F("animationName")},q.csstransforms=function(){return!!F("transform")},q.csstransitions=function(){return F("transition")},q.svg=function(){return!!b.createElementNS&&!!b.createElementNS(p.svg,"svg").createSVGRect};for(var G in q)y(q,G)&&(v=G.toLowerCase(),e[v]=q[G](),t.push((e[v]?"":"no-")+v));return e.addTest=function(a,b){if(typeof a=="object")for(var d in a)y(a,d)&&e.addTest(d,a[d]);else{a=a.toLowerCase();if(e[a]!==c)return e;b=typeof b=="function"?b():b,typeof enableClasses!="undefined"&&enableClasses&&(f.className+=" "+(b?"":"no-")+a),e[a]=b}return e},z(""),h=j=null,e._version=d,e._prefixes=l,e._domPrefixes=o,e._cssomPrefixes=n,e.testProp=function(a){return D([a])},e.testAllProps=F,e.testStyles=w,e.prefixed=function(a,b,c){return b?F(a,b,c):F(a,"pfx")},e}(window,window.document);
/* jshint ignore:end */
// Add indexOf() support to arrays for Internet Explorer 8
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
if ( this === undefined || this === null ) {
throw new TypeError( '"this" is null or not defined' );
}
// Hack to convert object.length to a UInt32
var length = this.length >>> 0;
fromIndex = +fromIndex || 0;
if (Math.abs(fromIndex) === Infinity) {
fromIndex = 0;
}
if (fromIndex < 0) {
fromIndex += length;
if (fromIndex < 0) {
fromIndex = 0;
}
}
for (;fromIndex < length; fromIndex++) {
if (this[fromIndex] === searchElement) {
return fromIndex;
}
}
return -1;
};
}
/**
* Determine the prefix to use for the pageVisibility API
*/
var hidden,
visibilityChange;
if (typeof document.hidden !== "undefined") {
// Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
/**
* Is an object an array?
*
* @param {Object} object - The object we want to test
* @api private
*/
function isArray(object) {
if (Object.prototype.toString.call(object) === '[object Array]') {
return true;
}else {
return false;
}
}
/**
* Extend object a with the properties of object b.
* If there's a conflict, object b takes precedence.
*
* @param {Object} a - The first object to merge
* @param {Object} b - The second object to merge (takes precedence)
* @api private
*/
function extend(a, b) {
for (var i in b) {
a[i] = b[i];
}
return a;
}
/**
* Get the values of an element's CSS property
*
* @param {HTMLObject} element - The element to get the value from
* @param {String} property - The CSS property to get the value of
* @returns {String} value - The value from the element's CSS property
*/
function getStyle(element, property) {
var value;
// IE
if (element.currentStyle) {
value = element.currentStyle[property];
}
else if (document.defaultView && document.defaultView.getComputedStyle) {
value = document.defaultView.getComputedStyle(element, "")[property];
}
return value;
}
/**
* Cross Browser helper for addEventListener
*
* @param {HTMLObject} element - The element to attach the event to
* @param {String} eventName - The name of the event; "click" for example
* @param {Function} handler - The function to execute when the event occurs
* @returns {Function} handler - Returns the handler so it can be removed
*/
function addEvent(element, eventName, handler) {
if (element.addEventListener) {
element.addEventListener(eventName, handler, false);
return handler;
}
else if (element.attachEvent) {
// Allows IE to return this keyword
var handlerr = function() {
handler.call(element);
};
element.attachEvent("on" + eventName, handlerr);
return handlerr;
}
}
/**
* Cross Browser helper for removeEventListener
*
* @param {HTMLObject} element - The element to attach the event to
* @param {String} eventName - The name of the event; "click" for example
* @param {Function} handler - The function to execute when the event occurs
*/
function removeEvent(element, eventName, handler) {
if (element.addEventListener) {
element.removeEventListener(eventName, handler, false);
}
else if (element.detachEvent) {
element.detachEvent("on" + eventName, handler);
}
}
/**
* Converts a time value taken from a CSS property, such as "0.5s"
* and converts it to a number in milliseconds, such as 500
*
* @param {String} time - the time in a string
* @returns {Number} convertedTime - the time as a number
* @api private
*/
function convertTimeToMs(time) {
var convertedTime,
fraction;
// Deal with milliseconds and seconds
if (time.indexOf("ms") > -1) {
fraction = 1;
}else {
fraction = 1000;
}
if (time == "0s") {
convertedTime = 0;
}else {
convertedTime = parseFloat(time.replace("s", "")) * fraction;
}
return convertedTime;
}
/**
* Does an element have a particular class?
*
* @param {HTMLElement} element - The element to check
* @param {String} name - The name of the class to check for
* @returns {Boolean}
* @api private
*/
function hasClass(element, name) {
if (element === undefined) {
return;
}
return new RegExp('(\\s|^)' + name + '(\\s|$)').test(element.className);
}
/**
* Add a class to an element
*
* @param {Object} elements - The element(s) to add a class to
* @param {String} name - The class to add
* @api private
*/
function addClass(elements, name) {
var element,
elementsLength,
i;
// If only one element is defined, turn it into a nodelist so it'll pass
// through the for loop
if (isArray(elements) === false) {
elementsLength = 1;
elements = [elements];
}
elementsLength = elements.length;
for (i = 0; i < elementsLength; i++) {
element = elements[i];
if (hasClass(element, name) === false) {
element.className += (element.className ? ' ': '') + name;
}
}
}
/**
* Remove a class from an element
*
* @param {Object} elements - The element to remove a class from
* @param {String} name - The class to remove
* @api private
*/
function removeClass(elements, name) {
var element,
elementsLength,
i;
// If only one element is defined, turn it into a nodelist so it'll pass
// through the for loop
if (isArray(elements) === false) {
elementsLength = 1;
elements = [elements];
}
else {
elementsLength = elements.length;
}
for (i = 0; i < elementsLength; i++) {
element = elements[i];
if (hasClass(element, name) === true) {
element.className = element.className.replace(new RegExp('(\\s|^)' + name + '(\\s|$)'),' ').replace(/^\s+|\s+$/g, '');
}
}
}
/**
* Determine if the cursor is inside the boundaries of an
* element.
*
* @param {Object} element - The element to test
* @param {Object} cursor - The event holding cursor properties
*/
function insideElement(element, cursor) {
// Get the elements boundaries
var rect = element.getBoundingClientRect(),
inside = false;
// Return true if inside the boundaries of the Sequence element
if (cursor.clientX >= rect.left && cursor.clientX <= rect.right && cursor.clientY >= rect.top && cursor.clientY <= rect.bottom) {
inside = true;
}
return inside;
}
/**
* Determine if an element has a specified parent, and if so, return the
* index number for the element.
*
* The index is taken from the top level elements witint a pagination
* element. This function will iterate through each parent until it
* reaches the top level, then get all top level elements and determine
* the index of the chosen top level.
*
* @param {Object} parents - The parent element(s) that the child should be
* within
* @param {Object} target - The child element to test if it has the parent
* @param {Object} previousTarget - The element that was previously checked
* to determine if it was top level
* @api private
*/
function hasParent(parent, target, previousTarget) {
if (target.nodeName === "BODY") {
return false;
}
// We're at the pagination parent
if (parent === target) {
if (previousTarget !== undefined) {
// Get the top level element clicked and all top level elements
var topLevel = previousTarget;
var allTopLevel = parent.getElementsByTagName(topLevel.nodeName);
// Count the number of top level elements
var i = allTopLevel.length;
// Which top level element was clicked?
while (i--) {
if (topLevel === allTopLevel[i]) {
// One-base the index and return it
return i + 1;
}
}
}
}
// Not yet at the pagination parent element, iterate again
else {
previousTarget = target;
return hasParent(parent, target.parentNode, previousTarget);
}
}
/**
* Determine the Hammer direction required based on the swipe directions
* being used
* hammerjs.github.io/api/#directions
*
* @param {Object} swipeEvents - An object holding each swipe direction and
* the handler to execute
* @api private
*/
function getHammerDirection(swipeEvents) {
var swipeDirections = 0,
hammerDirection = Hammer.DIRECTION_NONE;
if (swipeEvents.left !== undefined || swipeEvents.right !== undefined) {
swipeDirections += 1;
}
if (swipeEvents.up !== undefined || swipeEvents.down !== undefined) {
swipeDirections += 2;
}
if (swipeDirections === 1) {
hammerDirection = Hammer.DIRECTION_HORIZONTAL;
} else if (swipeDirections === 2) {
hammerDirection = Hammer.DIRECTION_VERTICAL;
} else if (swipeDirections === 3) {
hammerDirection = Hammer.DIRECTION_ALL;
}
return hammerDirection;
}
/**
* Add classes to the Sequence container that allow for styling based on
* feature support.
*
* @param {HTMLObject} $el - The element to add the classes to
* @param {Object} Modernizr - Sequence's instance of Modernizr
* @api private
*/
function addFeatureSupportClasses($el, Modernizr) {
// TODO: Add support for all features used by Sequence and only manipulate
// the DOM once. Currently this function just adds a class for the touch
// feature. Probably best just to copy how Modernizr does it to implement
// full list of features
var prefix = "seq-",
support = "no-touch";
if (Modernizr.touch === true) {
support = "touch";
}
addClass($el, prefix + support);
}
/* --- PUBLIC PROPERTIES/METHODS --- */
// Expose some of Sequence's private properties
var self = {
modernizr: Modernizr
};
/**
* Manage UI elements such as nextButton, prevButton, and pagination
*/
self.ui = {
// Default UI elements
defaultElements: {
"nextButton": "seq-next",
"prevButton": "seq-prev",
"autoPlayButton": "seq-autoplay",
"pagination": "seq-pagination",
"preloader": "seq-preloader"
},
/**
* Get an UI element
*
* @param {String} type - The type of UI element (nextButton for example)
* @returns {Boolean | HTMLElement} option - True if using the default
* element, else an HTMLElement
*/
getElements: function(type, option) {
var element,
elements,
elementsLength,
relatedElements = [],
rel,
i;
// Get the element being used
if (option === true) {
// Default elements
elements = document.querySelectorAll("." + this.defaultElements[type]);
} else {
// Custom elements
elements = document.querySelectorAll(option);
}
elementsLength = elements.length;
// Does the element control this instance of Sequence? We're looking
// for either a global element or one with a rel attribute the same
// as this instances ID
for (i = 0; i < elementsLength; i++) {
element = elements[i];
rel = element.getAttribute("rel");
if (rel === null || rel === self.$container.getAttribute("id")) {
relatedElements.push(element);
}
}
return relatedElements;
},
/**
* Fade an element in using transitions if they're supported, else use JS
*
* @param {HTMLElement} element - The element to show
* @param {Number} duration - The duration to show the element over
*/
show: function(element, duration) {
if (self.propertySupport.transitions === true) {
element.style[Modernizr.prefixed("transitionDuration")] = duration + "ms";
element.style[Modernizr.prefixed("transitionProperty")] = "opacity, " + Modernizr.prefixed("transform");
element.style.opacity = 1;
}
else {
self.animationFallback.animate(element, "opacity", "", 0, 1, duration);
}
},
/**
* Fade an element out using transitions if they're supported, else use JS
*
* @param {HTMLElement} element - The element to hide
* @param {Number} duration - The duration to hide the element over
* @param {Function} callback - Function to execute when the element is
* hidden
*/
hide: function(element, duration, callback) {
if (self.propertySupport.transitions === true) {
element.style[Modernizr.prefixed("transitionDuration")] = duration + "ms";
element.style[Modernizr.prefixed("transitionProperty")] = "opacity, " + Modernizr.prefixed("transform");
element.style.opacity = 0;
}
else {
self.animationFallback.animate(element, "opacity", "", 1, 0, duration);
}
if (callback !== undefined) {
self.hideTimer = setTimeout(function() {
callback();
}, duration);
}
}
};
/**
* Methods relating to autoPlay
*/
self.autoPlay = {
/**
* Initiate autoPlay
*/
init: function() {
self.isAutoPlayPaused = false;
self.isAutoPlaying = false;
},
/**
* Determine the delay that should be applied before starting autoPlay. A
* custom delay should take precedence. If delay is true then the delay
* should use options.autoPlayDelay where specified. If not
* specified, use the same time as defined in options.autoPlayInterval
*
* @param {Boolean/Number} delay - Whether a delay should be applied before
* starting autoPlay (true = same amount as options.autoPlayInterval,
* false = no interval, number = custom interval period). Applied to
* autoPlay.start()
* @param {Number} startDelay - The delay applied via options.autoPlayDelay
* @param {Number} autoPlayInterval - The delay applied via options.autoPlayInterval
*/
getDelay: function(delay, startDelay, autoPlayInterval) {
switch (delay) {
case true:
delay = (startDelay === null) ? autoPlayInterval: startDelay;
break;
case false:
case undefined:
delay = 0;
break;
}
return delay;
},
/**
* Start autoPlay
*
* @param {Boolean/Number} delay - Whether a delay should be applied before
* starting autoPlay (true = same amount as options.autoPlayInterval,
* false = no delay, number = custom delay period)
* @param {Boolean} continuing - If autoPlay is continuing from a
* previous cycle, the started() callback won't be triggered
* @returns false - When autoPlay is already active and can't be started
* again
*/
start: function(delay, continuing) {
// Only start once
if (self.isAutoPlaying === true || self.isReady === false) {
return false;
}
var options = self.options;
// Which delay should we use?
delay = this.getDelay(delay, options.autoPlayDelay, options.autoPlayInterval);
// Callback (only to be triggered when autoPlay is continuing from a
// previous cycle)
if (continuing === undefined) {
self.started(self);
}
addClass(self.$container, "seq-autoplaying");
addClass(self.$autoPlay, "seq-autoplaying");
// autoPlay is now enabled and active
options.autoPlay = true;
self.isAutoPlaying = true;
// Only start a new autoPlay timer if Sequence isn't already animating.
// If it is, a new one will be started at the end of the animation.
if (self.isAnimating === false) {
// Choose the direction and start autoPlay
self.autoPlayTimer = setTimeout(function() {
if (options.autoPlayDirection === 1) {
self.next();
}else {
self.prev();
}
}, delay);
}
return true;
},
/**
* Stop autoPlay
*/
stop: function() {
if (self.options.autoPlay === true && self.isAutoPlaying === true) {
self.options.autoPlay = false;
self.isAutoPlaying = false;
clearTimeout(self.autoPlayTimer);
removeClass(self.$container, "seq-autoplaying");
removeClass(self.$autoPlay, "seq-autoplaying");
// Callback
self.stopped(self);
} else {
return false;
}
return true;
},
/**
* Unpause autoPlay
*
* autoPlay.pause() and autoPlay.unpause() are used internally to
* temporarily stop autoPlay when hovered over.
*/
unpause: function() {
if (self.isAutoPlayPaused === true) {
self.isAutoPlayPaused = false;
this.start(true);
} else {
return false;
}
return true;
},
/**
* Pause autoPlay
*
* autoPlay.pause() and autoPlay.unpause() are used internally to
* temporarily stop autoPlay when hovered over.
*/
pause: function() {
if (self.options.autoPlay === true) {
self.isAutoPlayPaused = true;
this.stop();
} else {
return false;
}
return true;
}
};
/**
* Controls Sequence's canvas
*/
self.canvas = {
/**
* Setup the canvas, screen, and steps ready to be animated
*/
init: function(id) {
if (self.$screen !== undefined) {
self.$screen.style.height = "100%";
self.$screen.style.width = "100%";
}
// Determine the position of each step and the transform properties
// required for the canvas so it can move to each step
self.canvas.getTransformProperties();
},
/**
* Get Sequence's steps
*
* @param {HTMLElement} canvas - The canvas element
* @returns {Array} steps - The elements that make up Sequence's steps
* @api private
*/
getSteps: function(canvas) {
var steps = [],
stepId,
step,
stepElements = canvas.children,
stepsLength = stepElements.length,
i;
// Where we'll save info about the animation
self.stepProperties = {};
// Get the steps that have a parent with a class of "seq-canvas"
for (i = 0; i < stepsLength; i++) {
step = stepElements[i];
stepId = i + 1;
steps.push(step);
// Add each step to the animation map, where we'll save its transform
// properties
self.stepProperties[stepId] = {};
self.stepProperties[stepId].isActive = false;
}
return steps;
},
/**