-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarouselize-1.0.0.js
187 lines (155 loc) · 6.28 KB
/
carouselize-1.0.0.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
/**
* Carouselize v1.0.0
* Hervé Benjamin - 12/25/2017
* License Creative Commons - BY SA - http://creativecommons.org/licenses/by-sa/4.0/
*/
(function ($) {
/**
* Dynamic Bootstrap 3.3.7 carousel constructor using fadeIn/fadeOut transition effects
*
* @param slidesData - The data objects to load (must be an array)
* @param autoSize - Set a max-width size relative to the size of the image being the smallest
* @param speedTransition - Set the speed of the transition when changing slides
* @param speed - The carousel cycle speed
*/
$.fn.carouselize = function (slidesData, autoSize = false, speed = 5000, speedTransition = 500) {
$element = $(this);
let interval = null;
/**
* Construct the carousel using Bootstrap classes
*/
let build = function () {
let baseIndicators = $('<ol></ol>')
.addClass('carousel-indicators');
let baseSlides = $('<div></div>')
.addClass('carousel-inner');
for (let i = 0; i < slidesData.length; i++) {
let indicator = $('<li></li>')
.attr('data-target', $element.id)
.attr('data-slide-to', i);
let img = $('<img src="' + slidesData[i].src + '">')
.attr('alt', slidesData[i].alt);
if (autoSize) {
$(img).on('load', function () {
$("<img/>") // Make in memory copy of image to avoid css issues
.attr("src", $(this).attr("src"))
.on('load', function () {
if ($element.css('maxWidth') === 'none'
|| $element.css('maxWidth') > this.width) {
$element.css('maxWidth', this.width);
}
});
});
}
let item = $('<div></div>')
.addClass('item')
.append(img);
if (0 === i) {
item.addClass('active');
indicator.addClass('active');
}
baseIndicators.append(indicator);
baseSlides.append(item);
}
$element.addClass('carousel slide')
.append(baseIndicators)
.append(baseSlides)
.append($('<a></a>')
.addClass('left carousel-control')
.attr('href', $element.id)
.attr('role', 'button')
.append($('<span></span>')
.addClass('glyphicon')
.addClass('glyphicon-chevron-left'))
.append($('<span></span>')
.addClass('sr-only')))
.append($('<a></a>')
.addClass('right carousel-control')
.attr('href', $element.id)
.attr('role', 'button')
.append($('<span></span>')
.addClass('glyphicon')
.addClass('glyphicon-chevron-right'))
.append($('<span></span>')
.addClass('sr-only')));
};
let slideTo = function (activeElement, targetElement, targetIndex) {
let indicators = $element.find('.carousel-indicators');
targetElement.opacity = 0;
realSpeed = Math.round(speedTransition / 2);
activeElement.fadeOut(realSpeed, function () {
activeElement.removeClass('active');
indicators.find('.active').removeClass('active');
$(indicators.children()[targetIndex]).addClass('active');
targetElement.fadeIn(realSpeed, function () {
targetElement.addClass('active');
})
});
};
let changeSlide = function (goRight = true) {
let items = $element.children('.carousel-inner').children('.item');
let activeItem = $element.find('.item.active');
let curPos = items.index(activeItem);
let nextPos = -1;
if (goRight) {
nextPos = curPos + 1;
if (nextPos >= items.length) {
nextPos = 0;
}
} else {
nextPos = curPos - 1;
if (nextPos < 0) {
nextPos = items.length - 1;
}
}
let nextItem = items.eq(nextPos);
slideTo(activeItem, nextItem, nextPos);
};
let pause = function () {
if (interval !== null) {
clearInterval(interval);
interval = null;
}
};
let cycle = function () {
if (interval == null) {
interval = setInterval($.proxy(changeSlide, $element), speed);
}
};
// Clear currents elements inside the carousel
$(this).empty();
// Construct the carousel elements
build();
$(this).find('.carousel-control').on('click', function () {
if ($(this).hasClass('left')) {
changeSlide(false);
} else {
changeSlide(true);
}
});
$(this).find('.carousel-indicators > li').on('click', function () {
let activeItem = $element.find('.item.active');
let items = $element.children('.carousel-inner').children('.item');
let targetIndex = $(this).attr('data-slide-to');
let targetItem = items.eq(targetIndex);
slideTo(activeItem, targetItem, targetIndex);
});
$(this).on('mouseenter', pause)
.on('mouseleave', cycle)
.on('keydown', function (e) {
switch (e.which) {
case 37:
changeSlide(false);
break;
case 39:
changeSlide();
break;
default:
return;
}
e.preventDefault();
});
// Start cycling
cycle();
}
})(jQuery);