-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlider.js
114 lines (97 loc) · 2.79 KB
/
Slider.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
var Slider = (function() {
function _Slider(sliderSelector) {
this.$slider = $(sliderSelector);
this.$slides = this.$slider.find('.slide');
this.$group = this.$slider.find('.slide-group');
this.$prevBtn = this.$slider.find('.slide-btn-prev');
this.$nextBtn = this.$slider.find('.slide-btn-next');
this.currentIndex = 0;
this.timer;
this.createBullets();
this.bindPrev();
this.bindNext();
this.autoMove();
}
_Slider.prototype.move = function(newIndex) {
var _this = this;
this.autoMove();
var slideLeft, animateLeft;
if (this.$group.is(':animated') || newIndex == this.currentIndex) return;
this.$slider.find('.slide-bullet').eq(_this.currentIndex).removeClass('active');
this.$slider.find('.slide-bullet').eq(newIndex).addClass('active');
if (newIndex > this.currentIndex) {
slideLeft = '100%';
animateLeft = '-100%';
} else {
slideLeft = '-100%';
animateLeft = '100%';
}
this.$slides.eq(newIndex).css({
display: 'block',
left: slideLeft
});
this.$group.animate({
left: animateLeft
}, function() {
_this.$slides.eq(_this.currentIndex).css({
display: 'none'
});
_this.$slides.eq(newIndex).css({
left: 0
});
_this.$group.css({
left: 0
});
_this.currentIndex = newIndex;
});
}
_Slider.prototype.autoMove = function() {
clearTimeout(this.timer);
var _this = this;
this.timer = setTimeout(function() {
if (_this.currentIndex < _this.$slides.length - 1) {
_this.move(_this.currentIndex + 1);
} else {
_this.move(0);
}
}, 3000);
}
_Slider.prototype.createBullets = function() {
var _this = this;
$.each(_this.$slides, function(index) {
var $btn = $('<button class="slide-bullet">•</button>');
$btn.appendTo(_this.$slider.find('.slide-bullets'));
$btn.on('click', function() {
_this.move(index);
});
if (index === _this.currentIndex) {
$btn.addClass('active');
}
});
}
_Slider.prototype.bindPrev = function() {
var _this = this;
this.$prevBtn.on('click', function() {
if (_this.currentIndex === 0) {
_this.move(_this.$slides.length - 1);
} else {
_this.move(_this.currentIndex - 1);
}
});
}
_Slider.prototype.bindNext = function() {
var _this = this;
this.$nextBtn.on('click', function() {
if (_this.currentIndex === _this.$slides.length - 1) {
_this.move(0);
} else {
_this.move(_this.currentIndex + 1);
}
});
}
return {
init: function(slider) {
new _Slider(slider);
}
}
})();