-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleSlide.js
80 lines (60 loc) · 1.93 KB
/
simpleSlide.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
(function ($) {
$.fn.simpleSlider = function(settings) {
if(!this.length) return false;
this.settings = $.extend(settings, $.fn.simpleSlider.defaults);
this.slider = $(this.settings.sliderSelector, this);
if(!this.slider.length) return false;
this.slides = $(this.settings.slideSelector, this.slider);
if(!this.slides.length) return false;
this.currentSlide = 0;
this.slideCount = this.slides.length;
var self = this;
this.go = function(idx, animation) {
var newSlide = self.slides.eq(idx);
if(!newSlide.length) return false;
var pos = newSlide.position();
pos.top *= -1;
pos.left *= -1;
self.trigger('slide_start', self.slides.eq(self.currentSlide), newSlide);
self.currentSlide = idx;
if(animation) {
self.slider.stop().animate(pos, this.settings.animationSpeed, function() {
self.trigger('slide_end', newSlide);
});
}
else {
self.slider.css(pos);
self.trigger('slide_end', newSlide);
}
if(self.currentSlide == 0){
self.trigger('slide_limit', 'start');
}
if(self.currentSlide == self.slides.length -1) {
self.trigger('slide_limit', 'end');
}
return true;
}
this.forward = function() {
var dest = self.currentSlide + 1;
if(dest == self.slideCount && self.settings.wrap)
dest = 0; //Wrap
return self.go(dest, true);
}
this.back = function() {
var dest = self.currentSlide -1;
if(dest < 0 && self.settings.wrap)
dest = self.slideCount -1; //Wrap
return self.go(self.currentSlide -1, true);
}
setTimeout(function() {
self.go(self.currentSlide, false);
}, 50);
return this;
}
$.fn.simpleSlider.defaults = {
slideSelector: 'li',
sliderSelector: 'ul',
animationSpeed: 1000,
wrap: true
}
})(jQuery);