-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarousel.js
129 lines (118 loc) · 3.73 KB
/
carousel.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
// Generated by CoffeeScript 1.6.3
(function() {
var Carousel;
Carousel = (function() {
function Carousel(element, options) {
var $element,
_this = this;
this.options = options;
this._current = 0;
this._sliding = false;
this._paused = false;
this._timer = null;
$element = $(element);
this.rect = $element.width();
this.$inner = $element.find(this.options.inner_selector);
this.$items = $element.find(this.options.items_selector);
if (this.options.pause) {
$element.on('mouseenter.py.carousel', function() {
return _this.pause(true);
}).on('mouseleave.py.carousel', function() {
return _this.pause(false);
});
}
if (this.options.auto) {
this.start();
}
this.$indicators = $(this.options.indicators_selector).each(function(i, ele) {
return $(ele).on('click.py.carousel', function() {
return _this.show(i);
});
});
$(this.options.control_next_selector).on('click.py.carousel', function() {
return _this.next();
});
$(this.options.control_prev_selector).on('click.py.carousel', function() {
return _this.prev();
});
}
Carousel.prototype.show = function(index) {
var $next, is_prev,
_this = this;
is_prev = arguments[1] === true ? true : false;
if (this._sliding) {
return false;
}
if ((index == null) || index === this._current) {
return false;
}
this._sliding = true;
this.$indicators.eq(this._current).removeClass('active');
this.$indicators.eq(index).addClass('active');
$next = this.$items.eq(index);
$next.addClass('active');
$next.css('left', is_prev ? "-" + this.rect + "px" : "" + this.rect + "px");
this.$inner.animate({
left: (is_prev ? this.rect : -this.rect)
}, this.options.duration, function() {
$next.css('left', '0px');
_this.$inner.css('left', '0px');
_this.$items.eq(_this._current).removeClass('active');
_this._current = index;
return _this._sliding = false;
});
return false;
};
Carousel.prototype.prev = function() {
var index;
index = this._current - 1 < 0 ? this.$items.length - 1 : this._current - 1;
return this.show(index, true);
};
Carousel.prototype.next = function() {
var index;
index = this._current + 1 > this.$items.length - 1 ? 0 : this._current + 1;
return this.show(index);
};
Carousel.prototype.start = function() {
var _this = this;
this.stop();
this._paused = false;
return this._timer = setInterval(function() {
if (!_this._paused) {
return _this.next();
}
}, this.options.delay);
};
Carousel.prototype.stop = function() {
if (this._timer) {
return this._timer = clearInterval(this._timer);
}
};
Carousel.prototype.pause = function(paused) {
return this._paused = paused;
};
return Carousel;
})();
$.fn.carousel = function(options) {
var defaults;
defaults = {
inner_selector: '.js-carousel-inner',
items_selector: '.js-carousel-inner .item',
indicators_selector: '.js-carousel-indicators .item',
control_next_selector: '.js-carousel-control.btn_next',
control_prev_selector: '.js-carousel-control.btn_prev',
delay: 6000,
duration: 500,
auto: true,
pause: true
};
options = $.extend(defaults, options);
return this.each(function() {
var $this;
$this = $(this);
if (!$this.data('py.carousel')) {
return $this.data('py.carousel', new Carousel(this, options));
}
});
};
}).call(this);