-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.mobile.carousel-0.0.1.js
232 lines (206 loc) · 5.62 KB
/
jquery.mobile.carousel-0.0.1.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
/*
* jQuery Mobile Carousel plugin
* Copyright (c) Kazuhiro Osawa
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function($) {
var name_space = 'mobile_carousel';
$.fn[name_space] = function() {
if (this.data("__attached") === "true") {
return;
}
// change style
this.addClass("ui-mobile-carousel");
var carousel_padding = 10;
var window_width = $(window).width();
var window_height = $(window).height();
var margin_width = Math.abs(window_width / 3);
var content_width = window_width - margin_width;
var left_base = -15 + (margin_width / 2);
this.each(function(){
var content_paging = $("<DIV/>")
.addClass("ui-mobile-carousel-content-paging");
// wrapping contents div
var content = $("<DIV/>")
.addClass("ui-mobile-carousel-content");
var ul = $(this)
.before(content);
ul.appendTo(content);
content.append(content_paging);
ul.height(window_height);
var elements = ul.find("li");
var initialize_cb = function(){
var li_left = 0;
elements.each(function(){
var li = $(this);
var img = li.find("img");
// fixed width, height
var image_width = content_width - carousel_padding;
if (image_width < img.width()) {
img.width(image_width);
}
if (window_height - carousel_padding < img.height()) {
img.height(window_height - carousel_padding);
}
li.width(content_width).height(window_height);
// fixed img position
li_left += window_height;
var left = (li.width() / 2) - (img.width() / 2);
var top = (li.height() / 2) - (img.height() / 2);
img.css({ left: left + "px", top: top + "px" });
});
var ul_width = elements.length * (window_width + 5) - (4 - elements.length) ;
ul.width(ul_width).css("left", left_base + "px");
};
// wait to all image load
var images = ul.find("img");
var loaded_count = 0;
var count_cb = function(){
loaded_count++;
if (images.length === loaded_count) {
initialize_cb();
}
};
images.each(function(){
if ($(this).width() > 0) {
loaded_count++;
}
});
if (images.length === loaded_count) {
initialize_cb();
} else {
images.load(count_cb).error(count_cb);
}
// paging setting
var currentIndex = 1;
var update_paging = function(idx) {
// set page marker
content_paging.empty();
var page_width = 0;
for (var i = 1; i < elements.length + 1; i++) {
var page = $("<SPAN/>");
if (i === currentIndex) {
page.text("●")
.addClass("ui-mobile-carousel-content-paging-hide");
} else {
page.text("○")
.addClass("ui-mobile-carousel-content-paging-show");
}
content_paging.append(page);
page_width += page.width();
}
// fixed position
content_paging.width(page_width);
var left = (window_width / 2) - (content_paging.width() / 2) - 15;
content_paging.css("left", left + "px");
content_paging.css("top", "-10px");
};
update_paging(currentIndex);
// touch events
var startX = 0,
startY = 0,
lastX = 0,
ulX = 0,
isScroll = null,
touching = false,
moved = true,
lastCurrentX = 0,
startMoveAt = 0,
lastMoveAt = 0;
var count = 0;
ul.touchstart(function(e){
if (touching) {
return;
}
var data = e.originalEvent.touches ?
e.originalEvent.touches[ 0 ] :
e;
startX = data.pageX;
startY = data.pageY;
touching = true;
moved = false;
if (currentIndex > 1) {
isScroll = true;
} else {
isScroll = null;
}
lastCurrentX = 0;
count = 0;
startMoveAt = lastMoveAt = (new Date).getTime();
});
ul.touchend(function(e){
if (touching === false) {
return;
}
var now = (new Date).getTime();
var after_index_fixup = function(){
update_paging(currentIndex);
var currentX = (content_width * (currentIndex - 1));
ul.animate({
"left": (left_base + (currentX * -1)) + "px"
}, 200, "swing", function(){
touching = false;
moved = true;
isScroll = null;
});
startMoveAt = lastMoveAt = 0;
};
if ((now - startMoveAt) < 300) {
var over = margin_width / 3 * -1;
if (lastX > 0 && elements.length > currentIndex) {
currentIndex++;
} else if (lastX < 0 && currentIndex > 1) {
currentIndex--;
over = over * -1;
}
var currentX = (content_width * (currentIndex - 1));
ul.animate({
"left": (left_base + (currentX * -1) + over) + "px"
}, 200, "swing", after_index_fixup);
} else {
if (Math.abs(lastX) > (window_width / 2)) {
if (lastX > 0 && elements.length > currentIndex) {
currentIndex++;
} else if (lastX < 0 && currentIndex > 1) {
currentIndex--;
}
}
after_index_fixup();
}
});
ul.touchmove(function(e){
if (moved) {
return;
}
var data = e.originalEvent.touches ?
e.originalEvent.touches[ 0 ] :
e;
if (isScroll === null) {
if (Math.abs(data.pageX - startX) > Math.abs(data.pageY - startY) * 1) {
isScroll = true;
} else {
isScroll = false;
}
}
if (isScroll === false) {
return;
}
touching = true;
e.preventDefault();
var now = (new Date).getTime();
/*
if (lastMoveAt > now - 10) {
return;
}
*/
lastMoveAt = now;
$.mobile.silentScroll(ul.offset().top);
lastX = startX - data.pageX;
var currentX = lastCurrentX = (content_width * (currentIndex - 1)) + lastX;
ul.css("left", (left_base + (currentX * -1)) + "px");
});
});
this.data("__attached", "true");
return this;
};
})(jQuery);