-
Notifications
You must be signed in to change notification settings - Fork 0
/
pawkycarousel.js
136 lines (100 loc) · 3.45 KB
/
pawkycarousel.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
jQuery.fn.pawkyCarousel = function(options) {
return this.each(function() {
new jQuery.pawkyCarousel(this, options);
});
};
jQuery.fn.pawkyAutoCarousel = function(options) {
return this.each(function() {
new jQuery.pawkyAutoCarousel(this, options);
});
};
/*
* Basic Carousel.
* Makes no assumptions about its content. Just moves a container inside a wrapper
*/
jQuery.pawkyCarousel = function(element, input_options) {
this.initialize(element, input_options);
}
jQuery.pawkyCarousel.prototype = {
initialize: function(element, input_options) {
this.items_container = $(element);
this.position = 0;
this.options = {
transition_duration: 1000,
transition_easing: "swing",
shift: 100,
width: this.items_container.width()
};
jQuery.extend(this.options, input_options);
},
slide: function(vector) {
this.position += vector * this.options.shift;
if (this.position >= this.options.width)
this.position = 0;
//reset to last page
if (this.position < 0){
var pages_count = Math.ceil( this.options.width / this.options.shift );
this.position = (pages_count-1) * this.options.shift;
}
// animation
this.carousel_transition(this.position);
},
carousel_transition: function(position) {
this.items_container.stop().animate({
"left": - position
},
{
"duration": this.options.transition_duration,
"easing": this.options.transition_easing
});
},
prev: function() {
this.slide(-1);
},
next: function() {
this.slide(1);
}
};
/*
* Auto Carousel.
* Creates wrappers and controls and resizes items
*/
jQuery.pawkyAutoCarousel = function(element, input_options) {
this.initialize(element, input_options);
}
jQuery.pawkyAutoCarousel.prototype = jQuery.extend( {}, jQuery.pawkyCarousel.prototype, {
initialize: function(element, input_options) {
var default_options = {
number_slides_visible: "1",
scroll: 1
};
var options = jQuery.extend({}, default_options, input_options);
//emulate: super(element,options)
jQuery.pawkyCarousel.prototype.initialize.call(this, element, options );
this.add_wrappers_and_controls();
this.setup_items_and_wrappers();
this.options.shift = this.options.scroll * this.item_width;
},
add_wrappers_and_controls: function() {
this.items_container.addClass("row_of_slides");
this.items_container.wrap('<div class="slide_holder_inner" />');
this.items_container.parent(".slide_holder_inner").wrap('<div class="slide_holder" />');
this.carousel_wrapper = this.items_container.parent(".slide_holder_inner").parent();
// add prev/next buttons
this.carousel_wrapper.append('<div class="prev_button"><</div>');
this.prev_button = this.carousel_wrapper.children(".prev_button");
this.carousel_wrapper.append('<div class="next_button">></div>');
this.next_button = this.carousel_wrapper.children(".next_button");
this.items_count = this.items_container.children().length;
// add events for buttons
var _this = this;
this.prev_button.click( function(){_this.prev();} );
this.next_button.click( function(){_this.next();} );
},
setup_items_and_wrappers: function(){
this.item_width = Math.floor(this.items_container.parent().width() / this.options.number_slides_visible);
this.options.width = Math.floor(this.items_count * this.item_width);
this.items_container.css('width', this.options.width);
this.items_container.children().css({width: this.item_width+"px", float: "left"});
}
});