forked from extremetrek/jQuery-Mobile-Carousel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.mobile.carousel.js
270 lines (236 loc) · 12 KB
/
jquery.mobile.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*!
* jQuery Mobile Carousel
* Source: https://github.com/blackdynamo/jQuery-Mobile-Carousel
* Demo: http://jsfiddle.net/blackdynamo/yxhzU/
* Blog: http://developingwithstyle.blogspot.com
*
* Copyright 2010, Donnovan Lewis
* Edits: Benjamin Gleitzman ([email protected])
* Licensed under the MIT
*/
(function ($) {
var methods = {
init: function (options) {
var settings = {
duration: 300,
direction: "horizontal",
minimumDrag: 20,
beforeStart: function () { },
afterStart: function () { },
beforeStop: function () { },
afterStop: function () { }
};
$.extend(settings, options || {});
return this.each(function () {
if (this.tagName.toLowerCase() != "ul") return;
var originalList = $(this);
var originalId = originalList[0].id;
var pages = originalList.children();
var width = originalList.parent().width();
var height = originalList.parent().height();
var navigator = $('#navigator');
var arrows = $('#arrows');
var dotlist = "";
//Css
var containerCss = { position: "relative", overflow: "hidden", width: width, height: height };
var listCss = { position: "relative", padding: "0", margin: "0", listStyle: "none", width: pages.length * width };
var listItemCss = { width: width, height: height };
var container = $("<div>").css(containerCss);
var list = $("<ul>").css(listCss);
list[0].id = originalId;
var currentPage = 1;
var start, stop;
list.data("settings", settings);
list.data("width", width);
list.data("list", list);
list.data("height", height);
list.data("currentPage", currentPage);
if (settings.direction.toLowerCase() === "horizontal") {
list.css({ float: "left" });
$.each(pages, function (i) {
var li = $("<li>")
.css($.extend(listItemCss, { float: "left" }))
.html($(this).html());
list.append(li);
dotlist = dotlist + '<span></span>';
});
navigator.html(dotlist);
navigator.find('span').first().addClass('on');
list.draggable({
axis: "x",
start: function (event) {
currentPage = list.data("currentPage");
settings.beforeStart.apply(list, arguments);
var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event;
start = {
coords: [event.originalEvent.clientX, event.originalEvent.clientY]
};
settings.afterStart.apply(list, arguments);
},
stop: function (event) {
settings.beforeStop.apply(list, arguments);
var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event;
stop = {
coords: [event.originalEvent.clientX, event.originalEvent.clientY]
};
start.coords[0] > stop.coords[0] ? moveLeft() : moveRight();
function moveLeft() {
if (currentPage === pages.length || dragDelta() < settings.minimumDrag) {
list.animate({ left: "+=" + dragDelta() }, settings.duration);
return;
}
var new_width = -1 * width * currentPage;
list.animate({ left: new_width }, settings.duration);
navigator.find('span').siblings().removeClass('on');
navigator.find('span').eq(currentPage).addClass('on');
currentPage++;
}
function moveRight() {
if (currentPage === 1 || dragDelta() < settings.minimumDrag) {
list.animate({ left: "-=" + dragDelta() }, settings.duration);
return;
}
var new_width = -1 * width * (currentPage - 1);
list.animate({ left: -1 * width * (currentPage - 2) }, settings.duration);
currentPage--;
navigator.find('span').siblings().removeClass('on');
navigator.find('span').eq(currentPage-1).addClass('on');
}
function dragDelta() {
return Math.abs(start.coords[0] - stop.coords[0]);
}
function adjustment() {
return width - dragDelta();
}
settings.afterStop.apply(list, arguments);
list.data("currentPage", currentPage);
}
});
} else if (settings.direction.toLowerCase() === "vertical") {
$.each(pages, function (i) {
var li = $("<li>")
.css(listItemCss)
.html($(this).html());
list.append(li);
dotlist = dotlist + '<span></span>';
});
navigator.addClass('vertical');
arrows.addClass('verticalarr');
navigator.html(dotlist);
navigator.find('span').first().addClass('on');
list.draggable({
axis: "y",
start: function (event) {
currentPage = list.data("currentPage");
settings.beforeStart.apply(list, arguments);
var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event;
start = {
coords: [event.originalEvent.clientX, event.originalEvent.clientY]
};
settings.afterStart.apply(list, arguments);
},
stop: function (event) {
settings.beforeStop.apply(list, arguments);
var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event;
stop = {
coords: [event.originalEvent.clientX, event.originalEvent.clientY]
};
start.coords[1] > stop.coords[1] ? moveUp() : moveDown();
function moveUp() {
if (currentPage === pages.length || dragDelta() < settings.minimumDrag) {
list.animate({ top: "+=" + dragDelta() }, settings.duration);
return;
}
var new_width = -1 * height * currentPage;
list.animate({ top: new_width }, settings.duration);
navigator.find('span').siblings().removeClass('on');
navigator.find('span').eq(currentPage).addClass('on');
currentPage++;
}
function moveDown() {
if (currentPage === 1 || dragDelta() < settings.minimumDrag) {
list.animate({ top: "-=" + dragDelta() }, settings.duration);
return;
}
var new_width = -1 * height * (currentPage - 2);
list.animate({ top: new_width }, settings.duration);
currentPage--;
navigator.find('span').siblings().removeClass('on');
navigator.find('span').eq(currentPage-1).addClass('on');
}
function dragDelta() {
return Math.abs(start.coords[1] - stop.coords[1]);
}
function adjustment() {
return height - dragDelta();
}
settings.afterStop.apply(list, arguments);
list.data("currentPage", currentPage);
}
});
}
container.append(list);
originalList.replaceWith(container);
});
},
next: function () {
var originalList = $(this);
var pages = originalList.children();
var settings = $(this).data("settings");
var width = $(this).data("width");
var list = $(this).data("list");
var height = $(this).data("height");
var currentPage = $(this).data("currentPage");
var navigator = $('#navigator');
if (currentPage === pages.length) {return;}
navigator.find('span').siblings().removeClass('on');
navigator.find('span').eq(currentPage).addClass('on');
if (settings.direction.toLowerCase() === "horizontal") {
var new_width = -1 * width * currentPage;
list.animate({ left: new_width }, settings.duration);
currentPage++;
}
else if (settings.direction.toLowerCase() === "vertical") {
var new_width = -1 * height * currentPage;
list.animate({ top: new_width }, settings.duration);
currentPage++;
}
$(this).data("currentPage", currentPage);
},
previous: function () {
var originalList = $(this);
var pages = originalList.children();
var settings = $(this).data("settings");
var width = $(this).data("width");
var list = $(this).data("list");
var height = $(this).data("height");
var currentPage = $(this).data("currentPage");
var navigator = $('#navigator');
if (currentPage === 1) {return;}
if (settings.direction.toLowerCase() === "horizontal") {
var new_width = -1 * width * (currentPage - 1);
list.animate({ left: -1 * width * (currentPage - 2) }, settings.duration);
currentPage--;
}
else if (settings.direction.toLowerCase() === "vertical") {
var new_width = -1 * height * (currentPage - 2);
list.animate({ top: new_width }, settings.duration);
currentPage--;
}
navigator.find('span').siblings().removeClass('on');
navigator.find('span').eq(currentPage-1).addClass('on');
$(this).data("currentPage", currentPage);
}
};
$.fn.carousel = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
else {
$.error('Method ' + method + ' does not exist in this jQuery plugin.');
}
};
})(jQuery);