-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslideshow.js
86 lines (74 loc) · 2.97 KB
/
slideshow.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
(function (root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define([], factory);
} else {
root.SlideShow = factory();
}
}(this, function () {
function createElement(tag, className, text) {
var elm = document.createElement(tag);
elm.className = className;
if (text) elm.appendChild(document.createTextNode(text));
return elm;
}
function SlideShow(selector, options) {
options = {
timeout: options && options.timeout || 5000
};
this.element = document.querySelector(selector);
if (!(this.element && this.element.children.length)) {
throw new Error('Element not found or no children.');
}
// Add slideshow classes
this.element.classList.add('slideshow');
this.element.classList.add('preload');
// Set the dimensions of the container based on image size
var elmImg = this.element.querySelector('img');
var doResize = function () {
this.element.style.height = elmImg.clientHeight + 'px';
}.bind(this);
doResize();
window.addEventListener('resize', doResize);
// Create caption elements from image properties
this._captions = [];
for (var i = 0; i < this.element.children.length; i++) {
var elmChild = this.element.children[i];
elmImg = elmChild.querySelector('img');
if (elmImg && elmImg.title) {
var elmCaption = createElement('div', 'caption');
elmCaption.appendChild(createElement('span', 'title', elmImg.title));
elmCaption.appendChild(createElement('span', 'alt', elmImg.alt));
elmChild.appendChild(elmCaption);
this._captions.push(elmCaption);
}
}
// Show the first slide
this.currentSlide = this.element.children[0];
this.currentSlide.classList.add('show-animation');
// Remove preload class to enable transition animations
setTimeout(function () {
this.element.classList.remove('preload');
}.bind(this));
// Start the slidehshow
var index = 0;
this._ticker = setInterval(function () {
this.currentSlide.classList.remove('show-animation');
index = (index + 1) % this.element.children.length;
this.currentSlide = this.element.children[index];
this.currentSlide.classList.add('show-animation');
}.bind(this), options.timeout);
}
SlideShow.prototype = {
constructor: SlideShow,
destroy: function () {
clearInterval(this._ticker);
this.currentSlide.classList.remove('show-animation');
for (var i = 0; i < this._captions.length; i++) {
this._captions[i].parentNode.removeChild(this._captions[i]);
}
this._captions = [];
}
}
return SlideShow;
}));