-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathminimal-carousel.js
More file actions
65 lines (59 loc) · 1.77 KB
/
minimal-carousel.js
File metadata and controls
65 lines (59 loc) · 1.77 KB
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
function Carousel(settings){
'use strict';
settings = settings || {};
this.carousel = document.querySelector(settings.carousel || '.carousel');
this.slides = this.carousel.querySelectorAll('ul li');
this.delay = settings.delay || 2.5;
this.autoplay = settings.autoplay === undefined ? true : settings.autoplay;
this.slides_total = this.slides.length;
this.current_slide = -1;
if (this.autoplay) {
this.play();
}
}
Carousel.prototype.next = function (is_interval_call) {
'use strict';
for (var s = 0; s < this.slides.length; s += 1) {
this.slides[s].style.display = 'none';
}
this.current_slide = (this.current_slide + 1) % this.slides.length;
this.slides[this.current_slide].style.display = 'block';
if (this.autoplay && this.interval && !is_interval_call) {
var that = this;
clearInterval(this.interval);
this.interval = setTimeout(function () {
that.play();
}, this.delay * 1000);
}
};
Carousel.prototype.prev = function () {
'use strict';
for (var s = 0; s < this.slides.length; s += 1) {
this.slides[s].style.display = 'none';
}
this.current_slide = Math.abs(this.current_slide - 1 + this.slides.length) % this.slides.length;
this.slides[this.current_slide].style.display = 'block';
if (this.autoplay && this.interval) {
var that = this;
clearInterval(this.interval);
this.interval = setTimeout(function () {
that.play();
}, this.delay * 1000);
}
};
Carousel.prototype.play = function () {
'use strict';
this.next(true);
var that = this;
this.autoplay = true;
this.interval = setTimeout(function () {
that.play();
}, this.delay * 1000);
};
Carousel.prototype.stop = function () {
'use strict';
if (this.interval) {
this.autoplay = false;
clearInterval(this.interval);
}
};